blob: 61e9210aa503ea4468c3452fbeef7a6ec0d72484 [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");
Ted Kremenek539311e2010-02-18 18:47:01 +0000407 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000408 }
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
Daniel Dunbard52864b2010-02-14 10:02:57 +0000441 CXCursor Cursor = MakeCXCursor(*I, TU);
442
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000443 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000444 SourceRange Range =
445 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
446 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000447 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000448
449 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000450 case RangeBefore:
451 // This declaration comes before the region of interest; skip it.
452 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000453
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000454 case RangeAfter:
455 // This declaration comes after the region of interest; we're done.
456 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000457
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000458 case RangeOverlap:
459 // This declaration overlaps the region of interest; visit it.
460 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000461 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000462 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000463
Daniel Dunbard52864b2010-02-14 10:02:57 +0000464 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000465 return true;
466 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000467
Douglas Gregorb1373d02010-01-20 20:59:29 +0000468 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000469}
470
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000471bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
472 llvm_unreachable("Translation units are visited directly by Visit()");
473 return false;
474}
475
476bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
477 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
478 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000479
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000480 return false;
481}
482
483bool CursorVisitor::VisitTagDecl(TagDecl *D) {
484 return VisitDeclContext(D);
485}
486
487bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
488 if (Expr *Init = D->getInitExpr())
489 return Visit(MakeCXCursor(Init, StmtParent, TU));
490 return false;
491}
492
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000493bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
494 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
495 if (Visit(TSInfo->getTypeLoc()))
496 return true;
497
498 return false;
499}
500
Douglas Gregorb1373d02010-01-20 20:59:29 +0000501bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000502 if (VisitDeclaratorDecl(ND))
503 return true;
504
Douglas Gregora59e3902010-01-21 23:27:09 +0000505 if (ND->isThisDeclarationADefinition() &&
506 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
507 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000508
Douglas Gregorb1373d02010-01-20 20:59:29 +0000509 return false;
510}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000511
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000512bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
513 if (VisitDeclaratorDecl(D))
514 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000515
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000516 if (Expr *BitWidth = D->getBitWidth())
517 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000518
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000519 return false;
520}
521
522bool CursorVisitor::VisitVarDecl(VarDecl *D) {
523 if (VisitDeclaratorDecl(D))
524 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000525
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000526 if (Expr *Init = D->getInit())
527 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000528
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000529 return false;
530}
531
532bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
533 // FIXME: We really need a TypeLoc covering Objective-C method declarations.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000534 // At the moment, we don't have information about locations in the return
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000535 // type.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000536 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000537 PEnd = ND->param_end();
538 P != PEnd; ++P) {
539 if (Visit(MakeCXCursor(*P, TU)))
540 return true;
541 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000542
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000543 if (ND->isThisDeclarationADefinition() &&
544 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
545 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000546
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000547 return false;
548}
549
Douglas Gregora59e3902010-01-21 23:27:09 +0000550bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
551 return VisitDeclContext(D);
552}
553
Douglas Gregorb1373d02010-01-20 20:59:29 +0000554bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000555 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
556 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000557 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000558
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000559 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
560 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
561 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000562 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000563 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000564
Douglas Gregora59e3902010-01-21 23:27:09 +0000565 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000566}
567
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000568bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
569 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
570 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
571 E = PID->protocol_end(); I != E; ++I, ++PL)
572 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
573 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000574
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000575 return VisitObjCContainerDecl(PID);
576}
577
Douglas Gregorb1373d02010-01-20 20:59:29 +0000578bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000579 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000580 if (D->getSuperClass() &&
581 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000582 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000583 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000584 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000585
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000586 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
587 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
588 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000589 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000590 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000591
Douglas Gregora59e3902010-01-21 23:27:09 +0000592 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000593}
594
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000595bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
596 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000597}
598
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000599bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000600 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000601 D->getLocation(), TU)))
602 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000603
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000604 return VisitObjCImplDecl(D);
605}
606
607bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
608#if 0
609 // Issue callbacks for super class.
610 // FIXME: No source location information!
611 if (D->getSuperClass() &&
612 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000613 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000614 TU)))
615 return true;
616#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000617
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000618 return VisitObjCImplDecl(D);
619}
620
621bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
622 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
623 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
624 E = D->protocol_end();
625 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000626 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000627 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000628
629 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000630}
631
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000632bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
633 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
634 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
635 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000636
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000637 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000638}
639
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000640bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
641 ASTContext &Context = TU->getASTContext();
642
643 // Some builtin types (such as Objective-C's "id", "sel", and
644 // "Class") have associated declarations. Create cursors for those.
645 QualType VisitType;
646 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
647 case BuiltinType::Void:
648 case BuiltinType::Bool:
649 case BuiltinType::Char_U:
650 case BuiltinType::UChar:
651 case BuiltinType::Char16:
652 case BuiltinType::Char32:
653 case BuiltinType::UShort:
654 case BuiltinType::UInt:
655 case BuiltinType::ULong:
656 case BuiltinType::ULongLong:
657 case BuiltinType::UInt128:
658 case BuiltinType::Char_S:
659 case BuiltinType::SChar:
660 case BuiltinType::WChar:
661 case BuiltinType::Short:
662 case BuiltinType::Int:
663 case BuiltinType::Long:
664 case BuiltinType::LongLong:
665 case BuiltinType::Int128:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000666 case BuiltinType::Float:
667 case BuiltinType::Double:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000668 case BuiltinType::LongDouble:
669 case BuiltinType::NullPtr:
670 case BuiltinType::Overload:
671 case BuiltinType::Dependent:
672 break;
673
674 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
675 break;
676
677 case BuiltinType::ObjCId:
678 VisitType = Context.getObjCIdType();
679 break;
680
681 case BuiltinType::ObjCClass:
682 VisitType = Context.getObjCClassType();
683 break;
684
685 case BuiltinType::ObjCSel:
686 VisitType = Context.getObjCSelType();
687 break;
688 }
689
690 if (!VisitType.isNull()) {
691 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000692 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000693 TU));
694 }
695
696 return false;
697}
698
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000699bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
700 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
701}
702
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000703bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
704 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
705}
706
707bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
708 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
709}
710
711bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
712 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
713 return true;
714
715 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
716 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
717 TU)))
718 return true;
719 }
720
721 return false;
722}
723
724bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
725 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
726 return true;
727
728 if (TL.hasProtocolsAsWritten()) {
729 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000730 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000731 TL.getProtocolLoc(I),
732 TU)))
733 return true;
734 }
735 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000736
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000737 return false;
738}
739
740bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
741 return Visit(TL.getPointeeLoc());
742}
743
744bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
745 return Visit(TL.getPointeeLoc());
746}
747
748bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
749 return Visit(TL.getPointeeLoc());
750}
751
752bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000753 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000754}
755
756bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000757 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000758}
759
760bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
761 if (Visit(TL.getResultLoc()))
762 return true;
763
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000764 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
765 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
766 return true;
767
768 return false;
769}
770
771bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
772 if (Visit(TL.getElementLoc()))
773 return true;
774
775 if (Expr *Size = TL.getSizeExpr())
776 return Visit(MakeCXCursor(Size, StmtParent, TU));
777
778 return false;
779}
780
Douglas Gregor2332c112010-01-21 20:48:56 +0000781bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
782 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
783}
784
785bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
786 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
787 return Visit(TSInfo->getTypeLoc());
788
789 return false;
790}
791
Douglas Gregora59e3902010-01-21 23:27:09 +0000792bool CursorVisitor::VisitStmt(Stmt *S) {
793 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
794 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000795 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000796 return true;
797 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000798
Douglas Gregora59e3902010-01-21 23:27:09 +0000799 return false;
800}
801
802bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
803 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
804 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000805 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000806 return true;
807 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000808
Douglas Gregora59e3902010-01-21 23:27:09 +0000809 return false;
810}
811
Douglas Gregorf5bab412010-01-22 01:00:11 +0000812bool CursorVisitor::VisitIfStmt(IfStmt *S) {
813 if (VarDecl *Var = S->getConditionVariable()) {
814 if (Visit(MakeCXCursor(Var, TU)))
815 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000816 }
817
Douglas Gregor263b47b2010-01-25 16:12:32 +0000818 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
819 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000820 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
821 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000822 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
823 return true;
824
825 return false;
826}
827
828bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
829 if (VarDecl *Var = S->getConditionVariable()) {
830 if (Visit(MakeCXCursor(Var, TU)))
831 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000832 }
833
Douglas Gregor263b47b2010-01-25 16:12:32 +0000834 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
835 return true;
836 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
837 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000838
Douglas Gregor263b47b2010-01-25 16:12:32 +0000839 return false;
840}
841
842bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
843 if (VarDecl *Var = S->getConditionVariable()) {
844 if (Visit(MakeCXCursor(Var, TU)))
845 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000846 }
847
Douglas Gregor263b47b2010-01-25 16:12:32 +0000848 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
849 return true;
850 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000851 return true;
852
Douglas Gregor263b47b2010-01-25 16:12:32 +0000853 return false;
854}
855
856bool CursorVisitor::VisitForStmt(ForStmt *S) {
857 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
858 return true;
859 if (VarDecl *Var = S->getConditionVariable()) {
860 if (Visit(MakeCXCursor(Var, TU)))
861 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000862 }
863
Douglas Gregor263b47b2010-01-25 16:12:32 +0000864 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
865 return true;
866 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
867 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000868 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
869 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000870
Douglas Gregorf5bab412010-01-22 01:00:11 +0000871 return false;
872}
873
Douglas Gregor336fd812010-01-23 00:40:08 +0000874bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
875 if (E->isArgumentType()) {
876 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
877 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000878
Douglas Gregor336fd812010-01-23 00:40:08 +0000879 return false;
880 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000881
Douglas Gregor336fd812010-01-23 00:40:08 +0000882 return VisitExpr(E);
883}
884
885bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
886 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
887 if (Visit(TSInfo->getTypeLoc()))
888 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000889
Douglas Gregor336fd812010-01-23 00:40:08 +0000890 return VisitCastExpr(E);
891}
892
893bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
894 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
895 if (Visit(TSInfo->getTypeLoc()))
896 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000897
Douglas Gregor336fd812010-01-23 00:40:08 +0000898 return VisitExpr(E);
899}
900
Ted Kremenek09dfa372010-02-18 05:46:33 +0000901bool CursorVisitor::VisitAttributes(Decl *D) {
902 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
903 if (Visit(MakeCXCursor(A, D, TU)))
904 return true;
905
906 return false;
907}
908
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000909extern "C" {
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000910CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000911 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000912 if (excludeDeclarationsFromPCH)
913 CIdxr->setOnlyLocalDecls();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000914 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000915}
916
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000917void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000918 if (CIdx)
919 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000920}
921
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000922void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000923 if (CIdx) {
924 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
925 CXXIdx->setUseExternalASTGeneration(value);
926 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000927}
928
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000929CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +0000930 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000931 if (!CIdx)
932 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000933
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000934 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000935
Douglas Gregor5352ac02010-01-28 00:27:43 +0000936 // Configure the diagnostics.
937 DiagnosticOptions DiagOpts;
938 llvm::OwningPtr<Diagnostic> Diags;
939 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000940 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000941 CXXIdx->getOnlyLocalDecls(),
942 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000943}
944
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000945CXTranslationUnit
946clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
947 const char *source_filename,
948 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000949 const char **command_line_args,
950 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000951 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000952 if (!CIdx)
953 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000954
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000955 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
956
Douglas Gregor5352ac02010-01-28 00:27:43 +0000957 // Configure the diagnostics.
958 DiagnosticOptions DiagOpts;
959 llvm::OwningPtr<Diagnostic> Diags;
960 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000961
Douglas Gregor4db64a42010-01-23 00:14:00 +0000962 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
963 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000964 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000965 = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
966 unsaved_files[I].Contents + unsaved_files[I].Length,
967 unsaved_files[I].Filename);
968 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
969 Buffer));
970 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000971
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000972 if (!CXXIdx->getUseExternalASTGeneration()) {
973 llvm::SmallVector<const char *, 16> Args;
974
975 // The 'source_filename' argument is optional. If the caller does not
976 // specify it then it is assumed that the source file is specified
977 // in the actual argument list.
978 if (source_filename)
979 Args.push_back(source_filename);
980 Args.insert(Args.end(), command_line_args,
981 command_line_args + num_command_line_args);
982
Douglas Gregor5352ac02010-01-28 00:27:43 +0000983 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000984
Ted Kremenek29b72842010-01-07 22:49:05 +0000985#ifdef USE_CRASHTRACER
986 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000987#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000988
Daniel Dunbar94220972009-12-05 02:17:18 +0000989 llvm::OwningPtr<ASTUnit> Unit(
990 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000991 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000992 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000993 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +0000994 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +0000995 RemappedFiles.size(),
996 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000997
Daniel Dunbar94220972009-12-05 02:17:18 +0000998 // FIXME: Until we have broader testing, just drop the entire AST if we
999 // encountered an error.
Douglas Gregor5352ac02010-01-28 00:27:43 +00001000 if (NumErrors != Diags->getNumErrors())
Daniel Dunbar94220972009-12-05 02:17:18 +00001001 return 0;
1002
1003 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001004 }
1005
Ted Kremenek139ba862009-10-22 00:03:57 +00001006 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001007 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001008
Ted Kremenek139ba862009-10-22 00:03:57 +00001009 // First add the complete path to the 'clang' executable.
1010 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001011 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001012
Ted Kremenek139ba862009-10-22 00:03:57 +00001013 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001014 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001015
Ted Kremenek139ba862009-10-22 00:03:57 +00001016 // The 'source_filename' argument is optional. If the caller does not
1017 // specify it then it is assumed that the source file is specified
1018 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001019 if (source_filename)
1020 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001021
Steve Naroff37b5ac22009-10-15 20:50:09 +00001022 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001023 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001024 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001025 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001026
Douglas Gregor4db64a42010-01-23 00:14:00 +00001027 // Remap any unsaved files to temporary files.
1028 std::vector<llvm::sys::Path> TemporaryFiles;
1029 std::vector<std::string> RemapArgs;
1030 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1031 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001032
Douglas Gregor4db64a42010-01-23 00:14:00 +00001033 // The pointers into the elements of RemapArgs are stable because we
1034 // won't be adding anything to RemapArgs after this point.
1035 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1036 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001037
Ted Kremenek139ba862009-10-22 00:03:57 +00001038 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1039 for (int i = 0; i < num_command_line_args; ++i)
1040 if (const char *arg = command_line_args[i]) {
1041 if (strcmp(arg, "-o") == 0) {
1042 ++i; // Also skip the matching argument.
1043 continue;
1044 }
1045 if (strcmp(arg, "-emit-ast") == 0 ||
1046 strcmp(arg, "-c") == 0 ||
1047 strcmp(arg, "-fsyntax-only") == 0) {
1048 continue;
1049 }
1050
1051 // Keep the argument.
1052 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001053 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001054
Douglas Gregord93256e2010-01-28 06:00:51 +00001055 // Generate a temporary name for the diagnostics file.
1056 char tmpFileResults[L_tmpnam];
1057 char *tmpResultsFileName = tmpnam(tmpFileResults);
1058 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1059 TemporaryFiles.push_back(DiagnosticsFile);
1060 argv.push_back("-fdiagnostics-binary");
1061
Ted Kremenek139ba862009-10-22 00:03:57 +00001062 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001063 argv.push_back(NULL);
1064
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001065 // Invoke 'clang'.
1066 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1067 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001068 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001069 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1070 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001071 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001072 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001073 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001074
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001075 if (!ErrMsg.empty()) {
1076 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001077 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001078 I != E; ++I) {
1079 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001080 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001081 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001082 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001083
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001084 Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001085 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001086
Douglas Gregor5352ac02010-01-28 00:27:43 +00001087 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001088 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001089 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001090 RemappedFiles.size(),
1091 /*CaptureDiagnostics=*/true);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001092 if (ATU)
1093 ATU->unlinkTemporaryFile();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001094
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001095 // FIXME: Currently we don't report diagnostics on invalid ASTs.
Douglas Gregora88084b2010-02-18 18:08:43 +00001096 if (ATU) {
1097 LoadSerializedDiagnostics(DiagnosticsFile,
1098 num_unsaved_files, unsaved_files,
1099 ATU->getFileManager(),
1100 ATU->getSourceManager(),
1101 ATU->getDiagnostics());
1102 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001103
Douglas Gregor4db64a42010-01-23 00:14:00 +00001104 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1105 TemporaryFiles[i].eraseFromDisk();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001106
Steve Naroffe19944c2009-10-15 22:23:48 +00001107 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001108}
1109
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001110void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001111 if (CTUnit)
1112 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001113}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001114
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001115CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001116 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001117 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001118
Steve Naroff77accc12009-09-03 18:19:54 +00001119 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001120 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001121}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001122
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001123CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001124 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001125 return Result;
1126}
1127
Ted Kremenekfb480492010-01-13 21:46:36 +00001128} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001129
Ted Kremenekfb480492010-01-13 21:46:36 +00001130//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001131// CXSourceLocation and CXSourceRange Operations.
1132//===----------------------------------------------------------------------===//
1133
Douglas Gregorb9790342010-01-22 21:44:22 +00001134extern "C" {
1135CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001136 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001137 return Result;
1138}
1139
1140unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001141 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1142 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1143 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001144}
1145
1146CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1147 CXFile file,
1148 unsigned line,
1149 unsigned column) {
1150 if (!tu)
1151 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001152
Douglas Gregorb9790342010-01-22 21:44:22 +00001153 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1154 SourceLocation SLoc
1155 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001156 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001157 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001158
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001159 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001160}
1161
Douglas Gregor5352ac02010-01-28 00:27:43 +00001162CXSourceRange clang_getNullRange() {
1163 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1164 return Result;
1165}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001166
Douglas Gregor5352ac02010-01-28 00:27:43 +00001167CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1168 if (begin.ptr_data[0] != end.ptr_data[0] ||
1169 begin.ptr_data[1] != end.ptr_data[1])
1170 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001171
1172 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001173 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001174 return Result;
1175}
1176
Douglas Gregor46766dc2010-01-26 19:19:08 +00001177void clang_getInstantiationLocation(CXSourceLocation location,
1178 CXFile *file,
1179 unsigned *line,
1180 unsigned *column,
1181 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001182 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1183
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001184 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001185 if (file)
1186 *file = 0;
1187 if (line)
1188 *line = 0;
1189 if (column)
1190 *column = 0;
1191 if (offset)
1192 *offset = 0;
1193 return;
1194 }
1195
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001196 const SourceManager &SM =
1197 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001198 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001199
1200 if (file)
1201 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1202 if (line)
1203 *line = SM.getInstantiationLineNumber(InstLoc);
1204 if (column)
1205 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001206 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001207 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001208}
1209
Douglas Gregor1db19de2010-01-19 21:36:55 +00001210CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001211 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001212 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001213 return Result;
1214}
1215
1216CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001217 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001218 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001219 return Result;
1220}
1221
Douglas Gregorb9790342010-01-22 21:44:22 +00001222} // end: extern "C"
1223
Douglas Gregor1db19de2010-01-19 21:36:55 +00001224//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001225// CXFile Operations.
1226//===----------------------------------------------------------------------===//
1227
1228extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001229CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001230 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001231 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001232
Steve Naroff88145032009-10-27 14:35:18 +00001233 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001234 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001235}
1236
1237time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001238 if (!SFile)
1239 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001240
Steve Naroff88145032009-10-27 14:35:18 +00001241 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1242 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001243}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001244
Douglas Gregorb9790342010-01-22 21:44:22 +00001245CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1246 if (!tu)
1247 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001248
Douglas Gregorb9790342010-01-22 21:44:22 +00001249 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001250
Douglas Gregorb9790342010-01-22 21:44:22 +00001251 FileManager &FMgr = CXXUnit->getFileManager();
1252 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1253 return const_cast<FileEntry *>(File);
1254}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001255
Ted Kremenekfb480492010-01-13 21:46:36 +00001256} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001257
Ted Kremenekfb480492010-01-13 21:46:36 +00001258//===----------------------------------------------------------------------===//
1259// CXCursor Operations.
1260//===----------------------------------------------------------------------===//
1261
Ted Kremenekfb480492010-01-13 21:46:36 +00001262static Decl *getDeclFromExpr(Stmt *E) {
1263 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1264 return RefExpr->getDecl();
1265 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1266 return ME->getMemberDecl();
1267 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1268 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001269
Ted Kremenekfb480492010-01-13 21:46:36 +00001270 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1271 return getDeclFromExpr(CE->getCallee());
1272 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1273 return getDeclFromExpr(CE->getSubExpr());
1274 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1275 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001276
Ted Kremenekfb480492010-01-13 21:46:36 +00001277 return 0;
1278}
1279
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001280static SourceLocation getLocationFromExpr(Expr *E) {
1281 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1282 return /*FIXME:*/Msg->getLeftLoc();
1283 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1284 return DRE->getLocation();
1285 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1286 return Member->getMemberLoc();
1287 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1288 return Ivar->getLocation();
1289 return E->getLocStart();
1290}
1291
Ted Kremenekfb480492010-01-13 21:46:36 +00001292extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001293
1294unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001295 CXCursorVisitor visitor,
1296 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001297 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001298
1299 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001300
Douglas Gregorb1373d02010-01-20 20:59:29 +00001301 // Set the PCHLevel to filter out unwanted decls if requested.
1302 if (CXXUnit->getOnlyLocalDecls()) {
1303 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001304
Douglas Gregorb1373d02010-01-20 20:59:29 +00001305 // If the main input was an AST, bump the level.
1306 if (CXXUnit->isMainFileAST())
1307 ++PCHLevel;
1308 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001309
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001310 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001311 return CursorVis.VisitChildren(parent);
1312}
1313
Douglas Gregor78205d42010-01-20 21:45:58 +00001314static CXString getDeclSpelling(Decl *D) {
1315 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1316 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001317 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001318
Douglas Gregor78205d42010-01-20 21:45:58 +00001319 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001320 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001321
Douglas Gregor78205d42010-01-20 21:45:58 +00001322 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1323 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1324 // and returns different names. NamedDecl returns the class name and
1325 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001326 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001327
Douglas Gregor78205d42010-01-20 21:45:58 +00001328 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001329 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001330
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001331 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001332}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001333
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001334CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001335 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001336 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001337
Steve Narofff334b4e2009-09-02 18:26:48 +00001338 if (clang_isReference(C.kind)) {
1339 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001340 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001341 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001342 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001343 }
1344 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001345 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001346 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001347 }
1348 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001349 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001350 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001351 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001352 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001353 case CXCursor_TypeRef: {
1354 TypeDecl *Type = getCursorTypeRef(C).first;
1355 assert(Type && "Missing type decl");
1356
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001357 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1358 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001359 }
1360
Daniel Dunbaracca7252009-11-30 20:42:49 +00001361 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001362 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001363 }
1364 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001365
1366 if (clang_isExpression(C.kind)) {
1367 Decl *D = getDeclFromExpr(getCursorExpr(C));
1368 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001369 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001370 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001371 }
1372
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001373 if (clang_isDeclaration(C.kind))
1374 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001375
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001376 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001377}
1378
Ted Kremeneke68fff62010-02-17 00:41:32 +00001379CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001380 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001381 case CXCursor_FunctionDecl:
1382 return createCXString("FunctionDecl");
1383 case CXCursor_TypedefDecl:
1384 return createCXString("TypedefDecl");
1385 case CXCursor_EnumDecl:
1386 return createCXString("EnumDecl");
1387 case CXCursor_EnumConstantDecl:
1388 return createCXString("EnumConstantDecl");
1389 case CXCursor_StructDecl:
1390 return createCXString("StructDecl");
1391 case CXCursor_UnionDecl:
1392 return createCXString("UnionDecl");
1393 case CXCursor_ClassDecl:
1394 return createCXString("ClassDecl");
1395 case CXCursor_FieldDecl:
1396 return createCXString("FieldDecl");
1397 case CXCursor_VarDecl:
1398 return createCXString("VarDecl");
1399 case CXCursor_ParmDecl:
1400 return createCXString("ParmDecl");
1401 case CXCursor_ObjCInterfaceDecl:
1402 return createCXString("ObjCInterfaceDecl");
1403 case CXCursor_ObjCCategoryDecl:
1404 return createCXString("ObjCCategoryDecl");
1405 case CXCursor_ObjCProtocolDecl:
1406 return createCXString("ObjCProtocolDecl");
1407 case CXCursor_ObjCPropertyDecl:
1408 return createCXString("ObjCPropertyDecl");
1409 case CXCursor_ObjCIvarDecl:
1410 return createCXString("ObjCIvarDecl");
1411 case CXCursor_ObjCInstanceMethodDecl:
1412 return createCXString("ObjCInstanceMethodDecl");
1413 case CXCursor_ObjCClassMethodDecl:
1414 return createCXString("ObjCClassMethodDecl");
1415 case CXCursor_ObjCImplementationDecl:
1416 return createCXString("ObjCImplementationDecl");
1417 case CXCursor_ObjCCategoryImplDecl:
1418 return createCXString("ObjCCategoryImplDecl");
1419 case CXCursor_UnexposedDecl:
1420 return createCXString("UnexposedDecl");
1421 case CXCursor_ObjCSuperClassRef:
1422 return createCXString("ObjCSuperClassRef");
1423 case CXCursor_ObjCProtocolRef:
1424 return createCXString("ObjCProtocolRef");
1425 case CXCursor_ObjCClassRef:
1426 return createCXString("ObjCClassRef");
1427 case CXCursor_TypeRef:
1428 return createCXString("TypeRef");
1429 case CXCursor_UnexposedExpr:
1430 return createCXString("UnexposedExpr");
1431 case CXCursor_DeclRefExpr:
1432 return createCXString("DeclRefExpr");
1433 case CXCursor_MemberRefExpr:
1434 return createCXString("MemberRefExpr");
1435 case CXCursor_CallExpr:
1436 return createCXString("CallExpr");
1437 case CXCursor_ObjCMessageExpr:
1438 return createCXString("ObjCMessageExpr");
1439 case CXCursor_UnexposedStmt:
1440 return createCXString("UnexposedStmt");
1441 case CXCursor_InvalidFile:
1442 return createCXString("InvalidFile");
1443 case CXCursor_NoDeclFound:
1444 return createCXString("NoDeclFound");
1445 case CXCursor_NotImplemented:
1446 return createCXString("NotImplemented");
1447 case CXCursor_TranslationUnit:
1448 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001449 case CXCursor_UnexposedAttr:
1450 return createCXString("UnexposedAttr");
1451 case CXCursor_IBActionAttr:
1452 return createCXString("attribute(ibaction)");
1453 case CXCursor_IBOutletAttr:
1454 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001455 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001456
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001457 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001458 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001459}
Steve Naroff89922f82009-08-31 00:59:03 +00001460
Ted Kremeneke68fff62010-02-17 00:41:32 +00001461enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1462 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001463 CXClientData client_data) {
1464 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1465 *BestCursor = cursor;
1466 return CXChildVisit_Recurse;
1467}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001468
Douglas Gregorb9790342010-01-22 21:44:22 +00001469CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1470 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001471 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001472
Douglas Gregorb9790342010-01-22 21:44:22 +00001473 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1474
Ted Kremeneka297de22010-01-25 22:34:44 +00001475 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001476 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1477 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001478 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001479
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001480 // FIXME: Would be great to have a "hint" cursor, then walk from that
1481 // hint cursor upward until we find a cursor whose source range encloses
1482 // the region of interest, rather than starting from the translation unit.
1483 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001484 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001485 Decl::MaxPCHLevel, RegionOfInterest);
1486 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001487 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001488 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001489}
1490
Ted Kremenek73885552009-11-17 19:28:59 +00001491CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001492 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001493}
1494
1495unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001496 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001497}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001498
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001499unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001500 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1501}
1502
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001503unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001504 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1505}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001506
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001507unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001508 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1509}
1510
Douglas Gregor97b98722010-01-19 23:20:36 +00001511unsigned clang_isExpression(enum CXCursorKind K) {
1512 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1513}
1514
1515unsigned clang_isStatement(enum CXCursorKind K) {
1516 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1517}
1518
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001519unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1520 return K == CXCursor_TranslationUnit;
1521}
1522
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001523CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001524 return C.kind;
1525}
1526
Douglas Gregor98258af2010-01-18 22:46:11 +00001527CXSourceLocation clang_getCursorLocation(CXCursor C) {
1528 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001529 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001530 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001531 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1532 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001533 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001534 }
1535
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001536 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001537 std::pair<ObjCProtocolDecl *, SourceLocation> P
1538 = getCursorObjCProtocolRef(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_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001543 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1544 = getCursorObjCClassRef(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 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001547
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001548 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001549 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001550 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001551 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001552
Douglas Gregorf46034a2010-01-18 23:41:10 +00001553 default:
1554 // FIXME: Need a way to enumerate all non-reference cases.
1555 llvm_unreachable("Missed a reference kind");
1556 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001557 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001558
1559 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001560 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001561 getLocationFromExpr(getCursorExpr(C)));
1562
Douglas Gregor5352ac02010-01-28 00:27:43 +00001563 if (!getCursorDecl(C))
1564 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001565
Douglas Gregorf46034a2010-01-18 23:41:10 +00001566 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001567 SourceLocation Loc = D->getLocation();
1568 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1569 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001570 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001571}
Douglas Gregora7bde202010-01-19 00:34:46 +00001572
1573CXSourceRange clang_getCursorExtent(CXCursor C) {
1574 if (clang_isReference(C.kind)) {
1575 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001576 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001577 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1578 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001579 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001580 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001581
1582 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001583 std::pair<ObjCProtocolDecl *, SourceLocation> P
1584 = getCursorObjCProtocolRef(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_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001589 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1590 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001591
Ted Kremeneka297de22010-01-25 22:34:44 +00001592 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001593 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001594
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001595 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001596 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001597 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001598 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001599
Douglas Gregora7bde202010-01-19 00:34:46 +00001600 default:
1601 // FIXME: Need a way to enumerate all non-reference cases.
1602 llvm_unreachable("Missed a reference kind");
1603 }
1604 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001605
1606 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001607 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001608 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001609
1610 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001611 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001612 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001613
Douglas Gregor5352ac02010-01-28 00:27:43 +00001614 if (!getCursorDecl(C))
1615 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001616
Douglas Gregora7bde202010-01-19 00:34:46 +00001617 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001618 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001619}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001620
1621CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001622 if (clang_isInvalid(C.kind))
1623 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001624
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001625 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001626 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001627 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001628
Douglas Gregor97b98722010-01-19 23:20:36 +00001629 if (clang_isExpression(C.kind)) {
1630 Decl *D = getDeclFromExpr(getCursorExpr(C));
1631 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001632 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001633 return clang_getNullCursor();
1634 }
1635
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001636 if (!clang_isReference(C.kind))
1637 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001638
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001639 switch (C.kind) {
1640 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001641 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001642
1643 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001644 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001645
1646 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001647 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001648
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001649 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001650 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001651
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001652 default:
1653 // We would prefer to enumerate all non-reference cursor kinds here.
1654 llvm_unreachable("Unhandled reference cursor kind");
1655 break;
1656 }
1657 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001658
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001659 return clang_getNullCursor();
1660}
1661
Douglas Gregorb6998662010-01-19 19:34:47 +00001662CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001663 if (clang_isInvalid(C.kind))
1664 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001665
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001666 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001667
Douglas Gregorb6998662010-01-19 19:34:47 +00001668 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001669 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001670 C = clang_getCursorReferenced(C);
1671 WasReference = true;
1672 }
1673
1674 if (!clang_isDeclaration(C.kind))
1675 return clang_getNullCursor();
1676
1677 Decl *D = getCursorDecl(C);
1678 if (!D)
1679 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001680
Douglas Gregorb6998662010-01-19 19:34:47 +00001681 switch (D->getKind()) {
1682 // Declaration kinds that don't really separate the notions of
1683 // declaration and definition.
1684 case Decl::Namespace:
1685 case Decl::Typedef:
1686 case Decl::TemplateTypeParm:
1687 case Decl::EnumConstant:
1688 case Decl::Field:
1689 case Decl::ObjCIvar:
1690 case Decl::ObjCAtDefsField:
1691 case Decl::ImplicitParam:
1692 case Decl::ParmVar:
1693 case Decl::NonTypeTemplateParm:
1694 case Decl::TemplateTemplateParm:
1695 case Decl::ObjCCategoryImpl:
1696 case Decl::ObjCImplementation:
1697 case Decl::LinkageSpec:
1698 case Decl::ObjCPropertyImpl:
1699 case Decl::FileScopeAsm:
1700 case Decl::StaticAssert:
1701 case Decl::Block:
1702 return C;
1703
1704 // Declaration kinds that don't make any sense here, but are
1705 // nonetheless harmless.
1706 case Decl::TranslationUnit:
1707 case Decl::Template:
1708 case Decl::ObjCContainer:
1709 break;
1710
1711 // Declaration kinds for which the definition is not resolvable.
1712 case Decl::UnresolvedUsingTypename:
1713 case Decl::UnresolvedUsingValue:
1714 break;
1715
1716 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001717 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1718 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001719
1720 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001721 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001722
1723 case Decl::Enum:
1724 case Decl::Record:
1725 case Decl::CXXRecord:
1726 case Decl::ClassTemplateSpecialization:
1727 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001728 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001729 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001730 return clang_getNullCursor();
1731
1732 case Decl::Function:
1733 case Decl::CXXMethod:
1734 case Decl::CXXConstructor:
1735 case Decl::CXXDestructor:
1736 case Decl::CXXConversion: {
1737 const FunctionDecl *Def = 0;
1738 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001739 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001740 return clang_getNullCursor();
1741 }
1742
1743 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001744 // Ask the variable if it has a definition.
1745 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1746 return MakeCXCursor(Def, CXXUnit);
1747 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001748 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001749
Douglas Gregorb6998662010-01-19 19:34:47 +00001750 case Decl::FunctionTemplate: {
1751 const FunctionDecl *Def = 0;
1752 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001753 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001754 return clang_getNullCursor();
1755 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001756
Douglas Gregorb6998662010-01-19 19:34:47 +00001757 case Decl::ClassTemplate: {
1758 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001759 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001760 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001761 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001762 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001763 return clang_getNullCursor();
1764 }
1765
1766 case Decl::Using: {
1767 UsingDecl *Using = cast<UsingDecl>(D);
1768 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001769 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1770 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001771 S != SEnd; ++S) {
1772 if (Def != clang_getNullCursor()) {
1773 // FIXME: We have no way to return multiple results.
1774 return clang_getNullCursor();
1775 }
1776
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001777 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001778 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001779 }
1780
1781 return Def;
1782 }
1783
1784 case Decl::UsingShadow:
1785 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001786 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001787 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001788
1789 case Decl::ObjCMethod: {
1790 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1791 if (Method->isThisDeclarationADefinition())
1792 return C;
1793
1794 // Dig out the method definition in the associated
1795 // @implementation, if we have it.
1796 // FIXME: The ASTs should make finding the definition easier.
1797 if (ObjCInterfaceDecl *Class
1798 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1799 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1800 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1801 Method->isInstanceMethod()))
1802 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001803 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001804
1805 return clang_getNullCursor();
1806 }
1807
1808 case Decl::ObjCCategory:
1809 if (ObjCCategoryImplDecl *Impl
1810 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001811 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001812 return clang_getNullCursor();
1813
1814 case Decl::ObjCProtocol:
1815 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1816 return C;
1817 return clang_getNullCursor();
1818
1819 case Decl::ObjCInterface:
1820 // There are two notions of a "definition" for an Objective-C
1821 // class: the interface and its implementation. When we resolved a
1822 // reference to an Objective-C class, produce the @interface as
1823 // the definition; when we were provided with the interface,
1824 // produce the @implementation as the definition.
1825 if (WasReference) {
1826 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1827 return C;
1828 } else if (ObjCImplementationDecl *Impl
1829 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001830 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001831 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001832
Douglas Gregorb6998662010-01-19 19:34:47 +00001833 case Decl::ObjCProperty:
1834 // FIXME: We don't really know where to find the
1835 // ObjCPropertyImplDecls that implement this property.
1836 return clang_getNullCursor();
1837
1838 case Decl::ObjCCompatibleAlias:
1839 if (ObjCInterfaceDecl *Class
1840 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1841 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001842 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001843
Douglas Gregorb6998662010-01-19 19:34:47 +00001844 return clang_getNullCursor();
1845
1846 case Decl::ObjCForwardProtocol: {
1847 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1848 if (Forward->protocol_size() == 1)
1849 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001850 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001851 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001852
1853 // FIXME: Cannot return multiple definitions.
1854 return clang_getNullCursor();
1855 }
1856
1857 case Decl::ObjCClass: {
1858 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1859 if (Class->size() == 1) {
1860 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1861 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001862 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001863 return clang_getNullCursor();
1864 }
1865
1866 // FIXME: Cannot return multiple definitions.
1867 return clang_getNullCursor();
1868 }
1869
1870 case Decl::Friend:
1871 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001872 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001873 return clang_getNullCursor();
1874
1875 case Decl::FriendTemplate:
1876 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001877 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001878 return clang_getNullCursor();
1879 }
1880
1881 return clang_getNullCursor();
1882}
1883
1884unsigned clang_isCursorDefinition(CXCursor C) {
1885 if (!clang_isDeclaration(C.kind))
1886 return 0;
1887
1888 return clang_getCursorDefinition(C) == C;
1889}
1890
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001891void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001892 const char **startBuf,
1893 const char **endBuf,
1894 unsigned *startLine,
1895 unsigned *startColumn,
1896 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001897 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001898 assert(getCursorDecl(C) && "CXCursor has null decl");
1899 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001900 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1901 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001902
Steve Naroff4ade6d62009-09-23 17:52:52 +00001903 SourceManager &SM = FD->getASTContext().getSourceManager();
1904 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1905 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1906 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1907 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1908 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1909 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1910}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001911
Ted Kremenekfb480492010-01-13 21:46:36 +00001912} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001913
Ted Kremenekfb480492010-01-13 21:46:36 +00001914//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001915// Token-based Operations.
1916//===----------------------------------------------------------------------===//
1917
1918/* CXToken layout:
1919 * int_data[0]: a CXTokenKind
1920 * int_data[1]: starting token location
1921 * int_data[2]: token length
1922 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001923 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001924 * otherwise unused.
1925 */
1926extern "C" {
1927
1928CXTokenKind clang_getTokenKind(CXToken CXTok) {
1929 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1930}
1931
1932CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1933 switch (clang_getTokenKind(CXTok)) {
1934 case CXToken_Identifier:
1935 case CXToken_Keyword:
1936 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001937 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1938 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001939
1940 case CXToken_Literal: {
1941 // We have stashed the starting pointer in the ptr_data field. Use it.
1942 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001943 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001944 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001945
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001946 case CXToken_Punctuation:
1947 case CXToken_Comment:
1948 break;
1949 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001950
1951 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001952 // deconstructing the source location.
1953 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1954 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001955 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001956
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001957 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
1958 std::pair<FileID, unsigned> LocInfo
1959 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
1960 std::pair<const char *,const char *> Buffer
1961 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
1962
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001963 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
1964 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001965}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001966
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001967CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
1968 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1969 if (!CXXUnit)
1970 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001971
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001972 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
1973 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1974}
1975
1976CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
1977 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00001978 if (!CXXUnit)
1979 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001980
1981 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001982 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1983}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001984
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001985void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1986 CXToken **Tokens, unsigned *NumTokens) {
1987 if (Tokens)
1988 *Tokens = 0;
1989 if (NumTokens)
1990 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001991
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001992 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1993 if (!CXXUnit || !Tokens || !NumTokens)
1994 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001995
Daniel Dunbar85b988f2010-02-14 08:31:57 +00001996 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001997 if (R.isInvalid())
1998 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001999
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002000 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2001 std::pair<FileID, unsigned> BeginLocInfo
2002 = SourceMgr.getDecomposedLoc(R.getBegin());
2003 std::pair<FileID, unsigned> EndLocInfo
2004 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002005
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002006 // Cannot tokenize across files.
2007 if (BeginLocInfo.first != EndLocInfo.first)
2008 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002009
2010 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002011 std::pair<const char *,const char *> Buffer
2012 = SourceMgr.getBufferData(BeginLocInfo.first);
2013 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2014 CXXUnit->getASTContext().getLangOptions(),
2015 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2016 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002017
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002018 // Lex tokens until we hit the end of the range.
2019 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2020 llvm::SmallVector<CXToken, 32> CXTokens;
2021 Token Tok;
2022 do {
2023 // Lex the next token
2024 Lex.LexFromRawLexer(Tok);
2025 if (Tok.is(tok::eof))
2026 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002027
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002028 // Initialize the CXToken.
2029 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002030
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002031 // - Common fields
2032 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2033 CXTok.int_data[2] = Tok.getLength();
2034 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002035
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002036 // - Kind-specific fields
2037 if (Tok.isLiteral()) {
2038 CXTok.int_data[0] = CXToken_Literal;
2039 CXTok.ptr_data = (void *)Tok.getLiteralData();
2040 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002041 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002042 std::pair<FileID, unsigned> LocInfo
2043 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002044 const char *StartPos
2045 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002046 LocInfo.second;
2047 IdentifierInfo *II
2048 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2049 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2050 CXToken_Identifier
2051 : CXToken_Keyword;
2052 CXTok.ptr_data = II;
2053 } else if (Tok.is(tok::comment)) {
2054 CXTok.int_data[0] = CXToken_Comment;
2055 CXTok.ptr_data = 0;
2056 } else {
2057 CXTok.int_data[0] = CXToken_Punctuation;
2058 CXTok.ptr_data = 0;
2059 }
2060 CXTokens.push_back(CXTok);
2061 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002062
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002063 if (CXTokens.empty())
2064 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002065
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002066 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2067 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2068 *NumTokens = CXTokens.size();
2069}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002070
2071typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2072
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002073enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2074 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002075 CXClientData client_data) {
2076 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2077
2078 // We only annotate the locations of declarations, simple
2079 // references, and expressions which directly reference something.
2080 CXCursorKind Kind = clang_getCursorKind(cursor);
2081 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2082 // Okay: We can annotate the location of this declaration with the
2083 // declaration or reference
2084 } else if (clang_isExpression(cursor.kind)) {
2085 if (Kind != CXCursor_DeclRefExpr &&
2086 Kind != CXCursor_MemberRefExpr &&
2087 Kind != CXCursor_ObjCMessageExpr)
2088 return CXChildVisit_Recurse;
2089
2090 CXCursor Referenced = clang_getCursorReferenced(cursor);
2091 if (Referenced == cursor || Referenced == clang_getNullCursor())
2092 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002093
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002094 // Okay: we can annotate the location of this expression
2095 } else {
2096 // Nothing to annotate
2097 return CXChildVisit_Recurse;
2098 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002099
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002100 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2101 (*Data)[Loc.int_data] = cursor;
2102 return CXChildVisit_Recurse;
2103}
2104
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002105void clang_annotateTokens(CXTranslationUnit TU,
2106 CXToken *Tokens, unsigned NumTokens,
2107 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002108 if (NumTokens == 0)
2109 return;
2110
2111 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002112 for (unsigned I = 0; I != NumTokens; ++I)
2113 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002114
2115 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2116 if (!CXXUnit || !Tokens)
2117 return;
2118
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002119 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002120 SourceRange RegionOfInterest;
2121 RegionOfInterest.setBegin(
2122 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2123 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002124 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002125 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002126 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002127 // FIXME: Would be great to have a "hint" cursor, then walk from that
2128 // hint cursor upward until we find a cursor whose source range encloses
2129 // the region of interest, rather than starting from the translation unit.
2130 AnnotateTokensData Annotated;
2131 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002132 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002133 Decl::MaxPCHLevel, RegionOfInterest);
2134 AnnotateVis.VisitChildren(Parent);
2135
2136 for (unsigned I = 0; I != NumTokens; ++I) {
2137 // Determine whether we saw a cursor at this token's location.
2138 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2139 if (Pos == Annotated.end())
2140 continue;
2141
2142 Cursors[I] = Pos->second;
2143 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002144}
2145
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002146void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002147 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002148 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002149}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002150
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002151} // end: extern "C"
2152
2153//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002154// CXString Operations.
2155//===----------------------------------------------------------------------===//
2156
2157extern "C" {
2158const char *clang_getCString(CXString string) {
2159 return string.Spelling;
2160}
2161
2162void clang_disposeString(CXString string) {
2163 if (string.MustFreeString && string.Spelling)
2164 free((void*)string.Spelling);
2165}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002166
Ted Kremenekfb480492010-01-13 21:46:36 +00002167} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002168
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002169namespace clang { namespace cxstring {
2170CXString createCXString(const char *String, bool DupString){
2171 CXString Str;
2172 if (DupString) {
2173 Str.Spelling = strdup(String);
2174 Str.MustFreeString = 1;
2175 } else {
2176 Str.Spelling = String;
2177 Str.MustFreeString = 0;
2178 }
2179 return Str;
2180}
2181
2182CXString createCXString(llvm::StringRef String, bool DupString) {
2183 CXString Result;
2184 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2185 char *Spelling = (char *)malloc(String.size() + 1);
2186 memmove(Spelling, String.data(), String.size());
2187 Spelling[String.size()] = 0;
2188 Result.Spelling = Spelling;
2189 Result.MustFreeString = 1;
2190 } else {
2191 Result.Spelling = String.data();
2192 Result.MustFreeString = 0;
2193 }
2194 return Result;
2195}
2196}}
2197
Ted Kremenek04bb7162010-01-22 22:44:15 +00002198//===----------------------------------------------------------------------===//
2199// Misc. utility functions.
2200//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002201
Ted Kremenek04bb7162010-01-22 22:44:15 +00002202extern "C" {
2203
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002204CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002205 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002206}
2207
2208} // end: extern "C"