blob: cb81988d9517b9571c431cbe68cb41c707b89629 [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 Gregora88084b2010-02-18 18:08:43 +0000936 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000937 if (!CIdx)
938 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000939
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000940 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000941
Douglas Gregor5352ac02010-01-28 00:27:43 +0000942 // Configure the diagnostics.
943 DiagnosticOptions DiagOpts;
944 llvm::OwningPtr<Diagnostic> Diags;
945 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000946 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000947 CXXIdx->getOnlyLocalDecls(),
948 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000949}
950
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000951CXTranslationUnit
952clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
953 const char *source_filename,
954 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000955 const char **command_line_args,
956 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000957 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000958 if (!CIdx)
959 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000960
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000961 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
962
Douglas Gregor5352ac02010-01-28 00:27:43 +0000963 // Configure the diagnostics.
964 DiagnosticOptions DiagOpts;
965 llvm::OwningPtr<Diagnostic> Diags;
966 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000967
Douglas Gregor4db64a42010-01-23 00:14:00 +0000968 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
969 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000970 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000971 = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
972 unsaved_files[I].Contents + unsaved_files[I].Length,
973 unsaved_files[I].Filename);
974 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
975 Buffer));
976 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000977
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000978 if (!CXXIdx->getUseExternalASTGeneration()) {
979 llvm::SmallVector<const char *, 16> Args;
980
981 // The 'source_filename' argument is optional. If the caller does not
982 // specify it then it is assumed that the source file is specified
983 // in the actual argument list.
984 if (source_filename)
985 Args.push_back(source_filename);
986 Args.insert(Args.end(), command_line_args,
987 command_line_args + num_command_line_args);
988
Douglas Gregor5352ac02010-01-28 00:27:43 +0000989 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000990
Ted Kremenek29b72842010-01-07 22:49:05 +0000991#ifdef USE_CRASHTRACER
992 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000993#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000994
Daniel Dunbar94220972009-12-05 02:17:18 +0000995 llvm::OwningPtr<ASTUnit> Unit(
996 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000997 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000998 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000999 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001000 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001001 RemappedFiles.size(),
1002 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001003
Daniel Dunbar94220972009-12-05 02:17:18 +00001004 // FIXME: Until we have broader testing, just drop the entire AST if we
1005 // encountered an error.
Douglas Gregor5352ac02010-01-28 00:27:43 +00001006 if (NumErrors != Diags->getNumErrors())
Daniel Dunbar94220972009-12-05 02:17:18 +00001007 return 0;
1008
1009 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001010 }
1011
Ted Kremenek139ba862009-10-22 00:03:57 +00001012 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001013 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001014
Ted Kremenek139ba862009-10-22 00:03:57 +00001015 // First add the complete path to the 'clang' executable.
1016 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001017 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001018
Ted Kremenek139ba862009-10-22 00:03:57 +00001019 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001020 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001021
Ted Kremenek139ba862009-10-22 00:03:57 +00001022 // The 'source_filename' argument is optional. If the caller does not
1023 // specify it then it is assumed that the source file is specified
1024 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001025 if (source_filename)
1026 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001027
Steve Naroff37b5ac22009-10-15 20:50:09 +00001028 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001029 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001030 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001031 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001032
Douglas Gregor4db64a42010-01-23 00:14:00 +00001033 // Remap any unsaved files to temporary files.
1034 std::vector<llvm::sys::Path> TemporaryFiles;
1035 std::vector<std::string> RemapArgs;
1036 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1037 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001038
Douglas Gregor4db64a42010-01-23 00:14:00 +00001039 // The pointers into the elements of RemapArgs are stable because we
1040 // won't be adding anything to RemapArgs after this point.
1041 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1042 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001043
Ted Kremenek139ba862009-10-22 00:03:57 +00001044 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1045 for (int i = 0; i < num_command_line_args; ++i)
1046 if (const char *arg = command_line_args[i]) {
1047 if (strcmp(arg, "-o") == 0) {
1048 ++i; // Also skip the matching argument.
1049 continue;
1050 }
1051 if (strcmp(arg, "-emit-ast") == 0 ||
1052 strcmp(arg, "-c") == 0 ||
1053 strcmp(arg, "-fsyntax-only") == 0) {
1054 continue;
1055 }
1056
1057 // Keep the argument.
1058 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001059 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001060
Douglas Gregord93256e2010-01-28 06:00:51 +00001061 // Generate a temporary name for the diagnostics file.
1062 char tmpFileResults[L_tmpnam];
1063 char *tmpResultsFileName = tmpnam(tmpFileResults);
1064 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1065 TemporaryFiles.push_back(DiagnosticsFile);
1066 argv.push_back("-fdiagnostics-binary");
1067
Ted Kremenek139ba862009-10-22 00:03:57 +00001068 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001069 argv.push_back(NULL);
1070
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001071 // Invoke 'clang'.
1072 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1073 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001074 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001075 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1076 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001077 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001078 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001079 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001080
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001081 if (!ErrMsg.empty()) {
1082 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001083 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001084 I != E; ++I) {
1085 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001086 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001087 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001088 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001089
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001090 Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001091 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001092
Douglas Gregor5352ac02010-01-28 00:27:43 +00001093 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001094 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001095 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001096 RemappedFiles.size(),
1097 /*CaptureDiagnostics=*/true);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001098 if (ATU)
1099 ATU->unlinkTemporaryFile();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001100
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001101 // FIXME: Currently we don't report diagnostics on invalid ASTs.
Douglas Gregora88084b2010-02-18 18:08:43 +00001102 if (ATU) {
1103 LoadSerializedDiagnostics(DiagnosticsFile,
1104 num_unsaved_files, unsaved_files,
1105 ATU->getFileManager(),
1106 ATU->getSourceManager(),
1107 ATU->getDiagnostics());
1108 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001109
Douglas Gregor4db64a42010-01-23 00:14:00 +00001110 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1111 TemporaryFiles[i].eraseFromDisk();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001112
Steve Naroffe19944c2009-10-15 22:23:48 +00001113 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001114}
1115
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001116void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001117 if (CTUnit)
1118 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001119}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001120
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001121CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001122 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001123 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001124
Steve Naroff77accc12009-09-03 18:19:54 +00001125 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001126 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001127}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001128
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001129CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001130 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001131 return Result;
1132}
1133
Ted Kremenekfb480492010-01-13 21:46:36 +00001134} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001135
Ted Kremenekfb480492010-01-13 21:46:36 +00001136//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001137// CXSourceLocation and CXSourceRange Operations.
1138//===----------------------------------------------------------------------===//
1139
Douglas Gregorb9790342010-01-22 21:44:22 +00001140extern "C" {
1141CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001142 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001143 return Result;
1144}
1145
1146unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001147 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1148 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1149 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001150}
1151
1152CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1153 CXFile file,
1154 unsigned line,
1155 unsigned column) {
1156 if (!tu)
1157 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001158
Douglas Gregorb9790342010-01-22 21:44:22 +00001159 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1160 SourceLocation SLoc
1161 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001162 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001163 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001164
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001165 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001166}
1167
Douglas Gregor5352ac02010-01-28 00:27:43 +00001168CXSourceRange clang_getNullRange() {
1169 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1170 return Result;
1171}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001172
Douglas Gregor5352ac02010-01-28 00:27:43 +00001173CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1174 if (begin.ptr_data[0] != end.ptr_data[0] ||
1175 begin.ptr_data[1] != end.ptr_data[1])
1176 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001177
1178 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001179 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001180 return Result;
1181}
1182
Douglas Gregor46766dc2010-01-26 19:19:08 +00001183void clang_getInstantiationLocation(CXSourceLocation location,
1184 CXFile *file,
1185 unsigned *line,
1186 unsigned *column,
1187 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001188 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1189
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001190 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001191 if (file)
1192 *file = 0;
1193 if (line)
1194 *line = 0;
1195 if (column)
1196 *column = 0;
1197 if (offset)
1198 *offset = 0;
1199 return;
1200 }
1201
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001202 const SourceManager &SM =
1203 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001204 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001205
1206 if (file)
1207 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1208 if (line)
1209 *line = SM.getInstantiationLineNumber(InstLoc);
1210 if (column)
1211 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001212 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001213 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001214}
1215
Douglas Gregor1db19de2010-01-19 21:36:55 +00001216CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001217 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001218 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001219 return Result;
1220}
1221
1222CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001223 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001224 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001225 return Result;
1226}
1227
Douglas Gregorb9790342010-01-22 21:44:22 +00001228} // end: extern "C"
1229
Douglas Gregor1db19de2010-01-19 21:36:55 +00001230//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001231// CXFile Operations.
1232//===----------------------------------------------------------------------===//
1233
1234extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001235CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001236 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001237 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001238
Steve Naroff88145032009-10-27 14:35:18 +00001239 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001240 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001241}
1242
1243time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001244 if (!SFile)
1245 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001246
Steve Naroff88145032009-10-27 14:35:18 +00001247 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1248 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001249}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001250
Douglas Gregorb9790342010-01-22 21:44:22 +00001251CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1252 if (!tu)
1253 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001254
Douglas Gregorb9790342010-01-22 21:44:22 +00001255 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001256
Douglas Gregorb9790342010-01-22 21:44:22 +00001257 FileManager &FMgr = CXXUnit->getFileManager();
1258 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1259 return const_cast<FileEntry *>(File);
1260}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001261
Ted Kremenekfb480492010-01-13 21:46:36 +00001262} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001263
Ted Kremenekfb480492010-01-13 21:46:36 +00001264//===----------------------------------------------------------------------===//
1265// CXCursor Operations.
1266//===----------------------------------------------------------------------===//
1267
Ted Kremenekfb480492010-01-13 21:46:36 +00001268static Decl *getDeclFromExpr(Stmt *E) {
1269 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1270 return RefExpr->getDecl();
1271 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1272 return ME->getMemberDecl();
1273 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1274 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001275
Ted Kremenekfb480492010-01-13 21:46:36 +00001276 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1277 return getDeclFromExpr(CE->getCallee());
1278 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1279 return getDeclFromExpr(CE->getSubExpr());
1280 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1281 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001282
Ted Kremenekfb480492010-01-13 21:46:36 +00001283 return 0;
1284}
1285
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001286static SourceLocation getLocationFromExpr(Expr *E) {
1287 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1288 return /*FIXME:*/Msg->getLeftLoc();
1289 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1290 return DRE->getLocation();
1291 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1292 return Member->getMemberLoc();
1293 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1294 return Ivar->getLocation();
1295 return E->getLocStart();
1296}
1297
Ted Kremenekfb480492010-01-13 21:46:36 +00001298extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001299
1300unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001301 CXCursorVisitor visitor,
1302 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001303 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001304
1305 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001306
Douglas Gregorb1373d02010-01-20 20:59:29 +00001307 // Set the PCHLevel to filter out unwanted decls if requested.
1308 if (CXXUnit->getOnlyLocalDecls()) {
1309 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001310
Douglas Gregorb1373d02010-01-20 20:59:29 +00001311 // If the main input was an AST, bump the level.
1312 if (CXXUnit->isMainFileAST())
1313 ++PCHLevel;
1314 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001315
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001316 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001317 return CursorVis.VisitChildren(parent);
1318}
1319
Douglas Gregor78205d42010-01-20 21:45:58 +00001320static CXString getDeclSpelling(Decl *D) {
1321 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1322 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001323 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001324
Douglas Gregor78205d42010-01-20 21:45:58 +00001325 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001326 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001327
Douglas Gregor78205d42010-01-20 21:45:58 +00001328 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1329 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1330 // and returns different names. NamedDecl returns the class name and
1331 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001332 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001333
Douglas Gregor78205d42010-01-20 21:45:58 +00001334 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001335 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001336
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001337 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001338}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001339
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001340CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001341 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001342 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001343
Steve Narofff334b4e2009-09-02 18:26:48 +00001344 if (clang_isReference(C.kind)) {
1345 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001346 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001347 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001348 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001349 }
1350 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001351 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001352 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001353 }
1354 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001355 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001356 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001357 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001358 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001359 case CXCursor_TypeRef: {
1360 TypeDecl *Type = getCursorTypeRef(C).first;
1361 assert(Type && "Missing type decl");
1362
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001363 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1364 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001365 }
1366
Daniel Dunbaracca7252009-11-30 20:42:49 +00001367 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001368 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001369 }
1370 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001371
1372 if (clang_isExpression(C.kind)) {
1373 Decl *D = getDeclFromExpr(getCursorExpr(C));
1374 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001375 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001376 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001377 }
1378
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001379 if (clang_isDeclaration(C.kind))
1380 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001381
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001382 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001383}
1384
Ted Kremeneke68fff62010-02-17 00:41:32 +00001385CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001386 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001387 case CXCursor_FunctionDecl:
1388 return createCXString("FunctionDecl");
1389 case CXCursor_TypedefDecl:
1390 return createCXString("TypedefDecl");
1391 case CXCursor_EnumDecl:
1392 return createCXString("EnumDecl");
1393 case CXCursor_EnumConstantDecl:
1394 return createCXString("EnumConstantDecl");
1395 case CXCursor_StructDecl:
1396 return createCXString("StructDecl");
1397 case CXCursor_UnionDecl:
1398 return createCXString("UnionDecl");
1399 case CXCursor_ClassDecl:
1400 return createCXString("ClassDecl");
1401 case CXCursor_FieldDecl:
1402 return createCXString("FieldDecl");
1403 case CXCursor_VarDecl:
1404 return createCXString("VarDecl");
1405 case CXCursor_ParmDecl:
1406 return createCXString("ParmDecl");
1407 case CXCursor_ObjCInterfaceDecl:
1408 return createCXString("ObjCInterfaceDecl");
1409 case CXCursor_ObjCCategoryDecl:
1410 return createCXString("ObjCCategoryDecl");
1411 case CXCursor_ObjCProtocolDecl:
1412 return createCXString("ObjCProtocolDecl");
1413 case CXCursor_ObjCPropertyDecl:
1414 return createCXString("ObjCPropertyDecl");
1415 case CXCursor_ObjCIvarDecl:
1416 return createCXString("ObjCIvarDecl");
1417 case CXCursor_ObjCInstanceMethodDecl:
1418 return createCXString("ObjCInstanceMethodDecl");
1419 case CXCursor_ObjCClassMethodDecl:
1420 return createCXString("ObjCClassMethodDecl");
1421 case CXCursor_ObjCImplementationDecl:
1422 return createCXString("ObjCImplementationDecl");
1423 case CXCursor_ObjCCategoryImplDecl:
1424 return createCXString("ObjCCategoryImplDecl");
1425 case CXCursor_UnexposedDecl:
1426 return createCXString("UnexposedDecl");
1427 case CXCursor_ObjCSuperClassRef:
1428 return createCXString("ObjCSuperClassRef");
1429 case CXCursor_ObjCProtocolRef:
1430 return createCXString("ObjCProtocolRef");
1431 case CXCursor_ObjCClassRef:
1432 return createCXString("ObjCClassRef");
1433 case CXCursor_TypeRef:
1434 return createCXString("TypeRef");
1435 case CXCursor_UnexposedExpr:
1436 return createCXString("UnexposedExpr");
1437 case CXCursor_DeclRefExpr:
1438 return createCXString("DeclRefExpr");
1439 case CXCursor_MemberRefExpr:
1440 return createCXString("MemberRefExpr");
1441 case CXCursor_CallExpr:
1442 return createCXString("CallExpr");
1443 case CXCursor_ObjCMessageExpr:
1444 return createCXString("ObjCMessageExpr");
1445 case CXCursor_UnexposedStmt:
1446 return createCXString("UnexposedStmt");
1447 case CXCursor_InvalidFile:
1448 return createCXString("InvalidFile");
1449 case CXCursor_NoDeclFound:
1450 return createCXString("NoDeclFound");
1451 case CXCursor_NotImplemented:
1452 return createCXString("NotImplemented");
1453 case CXCursor_TranslationUnit:
1454 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001455 case CXCursor_UnexposedAttr:
1456 return createCXString("UnexposedAttr");
1457 case CXCursor_IBActionAttr:
1458 return createCXString("attribute(ibaction)");
1459 case CXCursor_IBOutletAttr:
1460 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001461 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001462
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001463 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001464 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001465}
Steve Naroff89922f82009-08-31 00:59:03 +00001466
Ted Kremeneke68fff62010-02-17 00:41:32 +00001467enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1468 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001469 CXClientData client_data) {
1470 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1471 *BestCursor = cursor;
1472 return CXChildVisit_Recurse;
1473}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001474
Douglas Gregorb9790342010-01-22 21:44:22 +00001475CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1476 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001477 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001478
Douglas Gregorb9790342010-01-22 21:44:22 +00001479 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1480
Ted Kremeneka297de22010-01-25 22:34:44 +00001481 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001482 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1483 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001484 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001485
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001486 // FIXME: Would be great to have a "hint" cursor, then walk from that
1487 // hint cursor upward until we find a cursor whose source range encloses
1488 // the region of interest, rather than starting from the translation unit.
1489 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001490 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001491 Decl::MaxPCHLevel, RegionOfInterest);
1492 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001493 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001494 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001495}
1496
Ted Kremenek73885552009-11-17 19:28:59 +00001497CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001498 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001499}
1500
1501unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001502 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001503}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001504
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001505unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001506 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1507}
1508
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001509unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001510 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1511}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001512
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001513unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001514 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1515}
1516
Douglas Gregor97b98722010-01-19 23:20:36 +00001517unsigned clang_isExpression(enum CXCursorKind K) {
1518 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1519}
1520
1521unsigned clang_isStatement(enum CXCursorKind K) {
1522 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1523}
1524
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001525unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1526 return K == CXCursor_TranslationUnit;
1527}
1528
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001529CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001530 return C.kind;
1531}
1532
Douglas Gregor98258af2010-01-18 22:46:11 +00001533CXSourceLocation clang_getCursorLocation(CXCursor C) {
1534 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001535 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001536 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001537 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1538 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001539 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001540 }
1541
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001542 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001543 std::pair<ObjCProtocolDecl *, SourceLocation> P
1544 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001545 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001546 }
1547
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001548 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001549 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1550 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001551 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001552 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001553
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001554 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001555 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001556 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001557 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001558
Douglas Gregorf46034a2010-01-18 23:41:10 +00001559 default:
1560 // FIXME: Need a way to enumerate all non-reference cases.
1561 llvm_unreachable("Missed a reference kind");
1562 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001563 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001564
1565 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001566 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001567 getLocationFromExpr(getCursorExpr(C)));
1568
Douglas Gregor5352ac02010-01-28 00:27:43 +00001569 if (!getCursorDecl(C))
1570 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001571
Douglas Gregorf46034a2010-01-18 23:41:10 +00001572 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001573 SourceLocation Loc = D->getLocation();
1574 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1575 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001576 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001577}
Douglas Gregora7bde202010-01-19 00:34:46 +00001578
1579CXSourceRange clang_getCursorExtent(CXCursor C) {
1580 if (clang_isReference(C.kind)) {
1581 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001582 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001583 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1584 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001585 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001586 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001587
1588 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001589 std::pair<ObjCProtocolDecl *, SourceLocation> P
1590 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001591 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001592 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001593
1594 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001595 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1596 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001597
Ted Kremeneka297de22010-01-25 22:34:44 +00001598 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001599 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001600
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001601 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001602 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001603 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001604 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001605
Douglas Gregora7bde202010-01-19 00:34:46 +00001606 default:
1607 // FIXME: Need a way to enumerate all non-reference cases.
1608 llvm_unreachable("Missed a reference kind");
1609 }
1610 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001611
1612 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001613 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001614 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001615
1616 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001617 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001618 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001619
Douglas Gregor5352ac02010-01-28 00:27:43 +00001620 if (!getCursorDecl(C))
1621 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001622
Douglas Gregora7bde202010-01-19 00:34:46 +00001623 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001624 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001625}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001626
1627CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001628 if (clang_isInvalid(C.kind))
1629 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001630
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001631 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001632 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001633 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001634
Douglas Gregor97b98722010-01-19 23:20:36 +00001635 if (clang_isExpression(C.kind)) {
1636 Decl *D = getDeclFromExpr(getCursorExpr(C));
1637 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001638 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001639 return clang_getNullCursor();
1640 }
1641
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001642 if (!clang_isReference(C.kind))
1643 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001644
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001645 switch (C.kind) {
1646 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001647 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001648
1649 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001650 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001651
1652 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001653 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001654
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001655 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001656 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001657
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001658 default:
1659 // We would prefer to enumerate all non-reference cursor kinds here.
1660 llvm_unreachable("Unhandled reference cursor kind");
1661 break;
1662 }
1663 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001664
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001665 return clang_getNullCursor();
1666}
1667
Douglas Gregorb6998662010-01-19 19:34:47 +00001668CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001669 if (clang_isInvalid(C.kind))
1670 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001671
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001672 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001673
Douglas Gregorb6998662010-01-19 19:34:47 +00001674 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001675 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001676 C = clang_getCursorReferenced(C);
1677 WasReference = true;
1678 }
1679
1680 if (!clang_isDeclaration(C.kind))
1681 return clang_getNullCursor();
1682
1683 Decl *D = getCursorDecl(C);
1684 if (!D)
1685 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001686
Douglas Gregorb6998662010-01-19 19:34:47 +00001687 switch (D->getKind()) {
1688 // Declaration kinds that don't really separate the notions of
1689 // declaration and definition.
1690 case Decl::Namespace:
1691 case Decl::Typedef:
1692 case Decl::TemplateTypeParm:
1693 case Decl::EnumConstant:
1694 case Decl::Field:
1695 case Decl::ObjCIvar:
1696 case Decl::ObjCAtDefsField:
1697 case Decl::ImplicitParam:
1698 case Decl::ParmVar:
1699 case Decl::NonTypeTemplateParm:
1700 case Decl::TemplateTemplateParm:
1701 case Decl::ObjCCategoryImpl:
1702 case Decl::ObjCImplementation:
1703 case Decl::LinkageSpec:
1704 case Decl::ObjCPropertyImpl:
1705 case Decl::FileScopeAsm:
1706 case Decl::StaticAssert:
1707 case Decl::Block:
1708 return C;
1709
1710 // Declaration kinds that don't make any sense here, but are
1711 // nonetheless harmless.
1712 case Decl::TranslationUnit:
1713 case Decl::Template:
1714 case Decl::ObjCContainer:
1715 break;
1716
1717 // Declaration kinds for which the definition is not resolvable.
1718 case Decl::UnresolvedUsingTypename:
1719 case Decl::UnresolvedUsingValue:
1720 break;
1721
1722 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001723 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1724 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001725
1726 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001727 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001728
1729 case Decl::Enum:
1730 case Decl::Record:
1731 case Decl::CXXRecord:
1732 case Decl::ClassTemplateSpecialization:
1733 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001734 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001735 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001736 return clang_getNullCursor();
1737
1738 case Decl::Function:
1739 case Decl::CXXMethod:
1740 case Decl::CXXConstructor:
1741 case Decl::CXXDestructor:
1742 case Decl::CXXConversion: {
1743 const FunctionDecl *Def = 0;
1744 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001745 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001746 return clang_getNullCursor();
1747 }
1748
1749 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001750 // Ask the variable if it has a definition.
1751 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1752 return MakeCXCursor(Def, CXXUnit);
1753 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001754 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001755
Douglas Gregorb6998662010-01-19 19:34:47 +00001756 case Decl::FunctionTemplate: {
1757 const FunctionDecl *Def = 0;
1758 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001759 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001760 return clang_getNullCursor();
1761 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001762
Douglas Gregorb6998662010-01-19 19:34:47 +00001763 case Decl::ClassTemplate: {
1764 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001765 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001766 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001767 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001768 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001769 return clang_getNullCursor();
1770 }
1771
1772 case Decl::Using: {
1773 UsingDecl *Using = cast<UsingDecl>(D);
1774 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001775 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1776 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001777 S != SEnd; ++S) {
1778 if (Def != clang_getNullCursor()) {
1779 // FIXME: We have no way to return multiple results.
1780 return clang_getNullCursor();
1781 }
1782
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001783 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001784 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001785 }
1786
1787 return Def;
1788 }
1789
1790 case Decl::UsingShadow:
1791 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001792 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001793 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001794
1795 case Decl::ObjCMethod: {
1796 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1797 if (Method->isThisDeclarationADefinition())
1798 return C;
1799
1800 // Dig out the method definition in the associated
1801 // @implementation, if we have it.
1802 // FIXME: The ASTs should make finding the definition easier.
1803 if (ObjCInterfaceDecl *Class
1804 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1805 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1806 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1807 Method->isInstanceMethod()))
1808 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001809 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001810
1811 return clang_getNullCursor();
1812 }
1813
1814 case Decl::ObjCCategory:
1815 if (ObjCCategoryImplDecl *Impl
1816 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001817 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001818 return clang_getNullCursor();
1819
1820 case Decl::ObjCProtocol:
1821 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1822 return C;
1823 return clang_getNullCursor();
1824
1825 case Decl::ObjCInterface:
1826 // There are two notions of a "definition" for an Objective-C
1827 // class: the interface and its implementation. When we resolved a
1828 // reference to an Objective-C class, produce the @interface as
1829 // the definition; when we were provided with the interface,
1830 // produce the @implementation as the definition.
1831 if (WasReference) {
1832 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1833 return C;
1834 } else if (ObjCImplementationDecl *Impl
1835 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001836 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001837 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001838
Douglas Gregorb6998662010-01-19 19:34:47 +00001839 case Decl::ObjCProperty:
1840 // FIXME: We don't really know where to find the
1841 // ObjCPropertyImplDecls that implement this property.
1842 return clang_getNullCursor();
1843
1844 case Decl::ObjCCompatibleAlias:
1845 if (ObjCInterfaceDecl *Class
1846 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1847 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001848 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001849
Douglas Gregorb6998662010-01-19 19:34:47 +00001850 return clang_getNullCursor();
1851
1852 case Decl::ObjCForwardProtocol: {
1853 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1854 if (Forward->protocol_size() == 1)
1855 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001856 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001857 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001858
1859 // FIXME: Cannot return multiple definitions.
1860 return clang_getNullCursor();
1861 }
1862
1863 case Decl::ObjCClass: {
1864 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1865 if (Class->size() == 1) {
1866 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1867 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001868 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001869 return clang_getNullCursor();
1870 }
1871
1872 // FIXME: Cannot return multiple definitions.
1873 return clang_getNullCursor();
1874 }
1875
1876 case Decl::Friend:
1877 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001878 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001879 return clang_getNullCursor();
1880
1881 case Decl::FriendTemplate:
1882 if (NamedDecl *Friend = cast<FriendTemplateDecl>(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
1887 return clang_getNullCursor();
1888}
1889
1890unsigned clang_isCursorDefinition(CXCursor C) {
1891 if (!clang_isDeclaration(C.kind))
1892 return 0;
1893
1894 return clang_getCursorDefinition(C) == C;
1895}
1896
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001897void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001898 const char **startBuf,
1899 const char **endBuf,
1900 unsigned *startLine,
1901 unsigned *startColumn,
1902 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001903 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001904 assert(getCursorDecl(C) && "CXCursor has null decl");
1905 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001906 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1907 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001908
Steve Naroff4ade6d62009-09-23 17:52:52 +00001909 SourceManager &SM = FD->getASTContext().getSourceManager();
1910 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1911 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1912 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1913 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1914 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1915 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1916}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001917
Ted Kremenekfb480492010-01-13 21:46:36 +00001918} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001919
Ted Kremenekfb480492010-01-13 21:46:36 +00001920//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001921// Token-based Operations.
1922//===----------------------------------------------------------------------===//
1923
1924/* CXToken layout:
1925 * int_data[0]: a CXTokenKind
1926 * int_data[1]: starting token location
1927 * int_data[2]: token length
1928 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001929 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001930 * otherwise unused.
1931 */
1932extern "C" {
1933
1934CXTokenKind clang_getTokenKind(CXToken CXTok) {
1935 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1936}
1937
1938CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1939 switch (clang_getTokenKind(CXTok)) {
1940 case CXToken_Identifier:
1941 case CXToken_Keyword:
1942 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001943 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1944 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001945
1946 case CXToken_Literal: {
1947 // We have stashed the starting pointer in the ptr_data field. Use it.
1948 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001949 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001950 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001951
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001952 case CXToken_Punctuation:
1953 case CXToken_Comment:
1954 break;
1955 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001956
1957 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001958 // deconstructing the source location.
1959 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1960 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001961 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001962
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001963 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
1964 std::pair<FileID, unsigned> LocInfo
1965 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
1966 std::pair<const char *,const char *> Buffer
1967 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
1968
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001969 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
1970 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001971}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001972
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001973CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
1974 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1975 if (!CXXUnit)
1976 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001977
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001978 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
1979 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1980}
1981
1982CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
1983 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00001984 if (!CXXUnit)
1985 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001986
1987 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001988 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1989}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001990
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001991void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1992 CXToken **Tokens, unsigned *NumTokens) {
1993 if (Tokens)
1994 *Tokens = 0;
1995 if (NumTokens)
1996 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001997
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001998 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1999 if (!CXXUnit || !Tokens || !NumTokens)
2000 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002001
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002002 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002003 if (R.isInvalid())
2004 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002005
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002006 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2007 std::pair<FileID, unsigned> BeginLocInfo
2008 = SourceMgr.getDecomposedLoc(R.getBegin());
2009 std::pair<FileID, unsigned> EndLocInfo
2010 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002011
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002012 // Cannot tokenize across files.
2013 if (BeginLocInfo.first != EndLocInfo.first)
2014 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002015
2016 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002017 std::pair<const char *,const char *> Buffer
2018 = SourceMgr.getBufferData(BeginLocInfo.first);
2019 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2020 CXXUnit->getASTContext().getLangOptions(),
2021 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2022 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002023
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002024 // Lex tokens until we hit the end of the range.
2025 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2026 llvm::SmallVector<CXToken, 32> CXTokens;
2027 Token Tok;
2028 do {
2029 // Lex the next token
2030 Lex.LexFromRawLexer(Tok);
2031 if (Tok.is(tok::eof))
2032 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002033
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002034 // Initialize the CXToken.
2035 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002036
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002037 // - Common fields
2038 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2039 CXTok.int_data[2] = Tok.getLength();
2040 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002041
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002042 // - Kind-specific fields
2043 if (Tok.isLiteral()) {
2044 CXTok.int_data[0] = CXToken_Literal;
2045 CXTok.ptr_data = (void *)Tok.getLiteralData();
2046 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002047 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002048 std::pair<FileID, unsigned> LocInfo
2049 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002050 const char *StartPos
2051 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002052 LocInfo.second;
2053 IdentifierInfo *II
2054 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2055 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2056 CXToken_Identifier
2057 : CXToken_Keyword;
2058 CXTok.ptr_data = II;
2059 } else if (Tok.is(tok::comment)) {
2060 CXTok.int_data[0] = CXToken_Comment;
2061 CXTok.ptr_data = 0;
2062 } else {
2063 CXTok.int_data[0] = CXToken_Punctuation;
2064 CXTok.ptr_data = 0;
2065 }
2066 CXTokens.push_back(CXTok);
2067 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002068
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002069 if (CXTokens.empty())
2070 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002071
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002072 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2073 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2074 *NumTokens = CXTokens.size();
2075}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002076
2077typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2078
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002079enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2080 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002081 CXClientData client_data) {
2082 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2083
2084 // We only annotate the locations of declarations, simple
2085 // references, and expressions which directly reference something.
2086 CXCursorKind Kind = clang_getCursorKind(cursor);
2087 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2088 // Okay: We can annotate the location of this declaration with the
2089 // declaration or reference
2090 } else if (clang_isExpression(cursor.kind)) {
2091 if (Kind != CXCursor_DeclRefExpr &&
2092 Kind != CXCursor_MemberRefExpr &&
2093 Kind != CXCursor_ObjCMessageExpr)
2094 return CXChildVisit_Recurse;
2095
2096 CXCursor Referenced = clang_getCursorReferenced(cursor);
2097 if (Referenced == cursor || Referenced == clang_getNullCursor())
2098 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002099
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002100 // Okay: we can annotate the location of this expression
2101 } else {
2102 // Nothing to annotate
2103 return CXChildVisit_Recurse;
2104 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002105
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002106 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2107 (*Data)[Loc.int_data] = cursor;
2108 return CXChildVisit_Recurse;
2109}
2110
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002111void clang_annotateTokens(CXTranslationUnit TU,
2112 CXToken *Tokens, unsigned NumTokens,
2113 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002114 if (NumTokens == 0)
2115 return;
2116
2117 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002118 for (unsigned I = 0; I != NumTokens; ++I)
2119 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002120
2121 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2122 if (!CXXUnit || !Tokens)
2123 return;
2124
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002125 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002126 SourceRange RegionOfInterest;
2127 RegionOfInterest.setBegin(
2128 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2129 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002130 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002131 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002132 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002133 // FIXME: Would be great to have a "hint" cursor, then walk from that
2134 // hint cursor upward until we find a cursor whose source range encloses
2135 // the region of interest, rather than starting from the translation unit.
2136 AnnotateTokensData Annotated;
2137 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002138 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002139 Decl::MaxPCHLevel, RegionOfInterest);
2140 AnnotateVis.VisitChildren(Parent);
2141
2142 for (unsigned I = 0; I != NumTokens; ++I) {
2143 // Determine whether we saw a cursor at this token's location.
2144 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2145 if (Pos == Annotated.end())
2146 continue;
2147
2148 Cursors[I] = Pos->second;
2149 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002150}
2151
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002152void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002153 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002154 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002155}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002156
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002157} // end: extern "C"
2158
2159//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002160// CXString Operations.
2161//===----------------------------------------------------------------------===//
2162
2163extern "C" {
2164const char *clang_getCString(CXString string) {
2165 return string.Spelling;
2166}
2167
2168void clang_disposeString(CXString string) {
2169 if (string.MustFreeString && string.Spelling)
2170 free((void*)string.Spelling);
2171}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002172
Ted Kremenekfb480492010-01-13 21:46:36 +00002173} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002174
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002175namespace clang { namespace cxstring {
2176CXString createCXString(const char *String, bool DupString){
2177 CXString Str;
2178 if (DupString) {
2179 Str.Spelling = strdup(String);
2180 Str.MustFreeString = 1;
2181 } else {
2182 Str.Spelling = String;
2183 Str.MustFreeString = 0;
2184 }
2185 return Str;
2186}
2187
2188CXString createCXString(llvm::StringRef String, bool DupString) {
2189 CXString Result;
2190 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2191 char *Spelling = (char *)malloc(String.size() + 1);
2192 memmove(Spelling, String.data(), String.size());
2193 Spelling[String.size()] = 0;
2194 Result.Spelling = Spelling;
2195 Result.MustFreeString = 1;
2196 } else {
2197 Result.Spelling = String.data();
2198 Result.MustFreeString = 0;
2199 }
2200 return Result;
2201}
2202}}
2203
Ted Kremenek04bb7162010-01-22 22:44:15 +00002204//===----------------------------------------------------------------------===//
2205// Misc. utility functions.
2206//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002207
Ted Kremenek04bb7162010-01-22 22:44:15 +00002208extern "C" {
2209
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002210CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002211 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002212}
2213
2214} // end: extern "C"