blob: 8bb6a0a7c5f162f133d21b97db1c0c0fac0cae9a [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 Gregor7d0d40e2010-01-21 16:28:34 +0000259 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000260 bool VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000261 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000262 bool VisitFunctionDecl(FunctionDecl *ND);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000263 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000264 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000265 bool VisitObjCClassDecl(ObjCClassDecl *D);
266 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
267 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
268 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
269 bool VisitObjCImplDecl(ObjCImplDecl *D);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000270 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000271 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
272 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000273 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
274 // etc.
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000275 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
276 bool VisitTagDecl(TagDecl *D);
277 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
278 bool VisitTypedefDecl(TypedefDecl *D);
279 bool VisitVarDecl(VarDecl *);
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()) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000647 case BuiltinType::Bool:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000648 case BuiltinType::Char16:
649 case BuiltinType::Char32:
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000650 case BuiltinType::Char_S:
651 case BuiltinType::Char_U:
652 case BuiltinType::Dependent:
653 case BuiltinType::Double:
654 case BuiltinType::Float:
655 case BuiltinType::Int128:
656 case BuiltinType::Int:
657 case BuiltinType::Long:
658 case BuiltinType::LongDouble:
659 case BuiltinType::LongLong:
660 case BuiltinType::NullPtr:
661 case BuiltinType::Overload:
662 case BuiltinType::SChar:
663 case BuiltinType::Short:
664 case BuiltinType::UChar:
665 case BuiltinType::UInt128:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000666 case BuiltinType::UInt:
667 case BuiltinType::ULong:
668 case BuiltinType::ULongLong:
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000669 case BuiltinType::UShort:
670 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000671 case BuiltinType::WChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000672 break;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000673 case BuiltinType::ObjCClass:
674 VisitType = Context.getObjCClassType();
675 break;
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000676 case BuiltinType::ObjCId:
677 VisitType = Context.getObjCIdType();
678 break;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000679 case BuiltinType::ObjCSel:
680 VisitType = Context.getObjCSelType();
681 break;
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000682 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
683 break;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000684 }
685
686 if (!VisitType.isNull()) {
687 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000688 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000689 TU));
690 }
691
692 return false;
693}
694
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000695bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
696 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
697}
698
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000699bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
700 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
701}
702
703bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
704 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
705}
706
707bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
708 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
709 return true;
710
711 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
712 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
713 TU)))
714 return true;
715 }
716
717 return false;
718}
719
720bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
721 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
722 return true;
723
724 if (TL.hasProtocolsAsWritten()) {
725 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000726 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000727 TL.getProtocolLoc(I),
728 TU)))
729 return true;
730 }
731 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000732
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000733 return false;
734}
735
736bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
737 return Visit(TL.getPointeeLoc());
738}
739
740bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
741 return Visit(TL.getPointeeLoc());
742}
743
744bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
745 return Visit(TL.getPointeeLoc());
746}
747
748bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000749 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000750}
751
752bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000753 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000754}
755
756bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
757 if (Visit(TL.getResultLoc()))
758 return true;
759
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000760 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
761 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
762 return true;
763
764 return false;
765}
766
767bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
768 if (Visit(TL.getElementLoc()))
769 return true;
770
771 if (Expr *Size = TL.getSizeExpr())
772 return Visit(MakeCXCursor(Size, StmtParent, TU));
773
774 return false;
775}
776
Douglas Gregor2332c112010-01-21 20:48:56 +0000777bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
778 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
779}
780
781bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
782 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
783 return Visit(TSInfo->getTypeLoc());
784
785 return false;
786}
787
Douglas Gregora59e3902010-01-21 23:27:09 +0000788bool CursorVisitor::VisitStmt(Stmt *S) {
789 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
790 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000791 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000792 return true;
793 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000794
Douglas Gregora59e3902010-01-21 23:27:09 +0000795 return false;
796}
797
798bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
799 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
800 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000801 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000802 return true;
803 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000804
Douglas Gregora59e3902010-01-21 23:27:09 +0000805 return false;
806}
807
Douglas Gregorf5bab412010-01-22 01:00:11 +0000808bool CursorVisitor::VisitIfStmt(IfStmt *S) {
809 if (VarDecl *Var = S->getConditionVariable()) {
810 if (Visit(MakeCXCursor(Var, TU)))
811 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000812 }
813
Douglas Gregor263b47b2010-01-25 16:12:32 +0000814 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
815 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000816 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
817 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000818 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
819 return true;
820
821 return false;
822}
823
824bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
825 if (VarDecl *Var = S->getConditionVariable()) {
826 if (Visit(MakeCXCursor(Var, TU)))
827 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000828 }
829
Douglas Gregor263b47b2010-01-25 16:12:32 +0000830 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
831 return true;
832 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
833 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000834
Douglas Gregor263b47b2010-01-25 16:12:32 +0000835 return false;
836}
837
838bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
839 if (VarDecl *Var = S->getConditionVariable()) {
840 if (Visit(MakeCXCursor(Var, TU)))
841 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000842 }
843
Douglas Gregor263b47b2010-01-25 16:12:32 +0000844 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
845 return true;
846 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000847 return true;
848
Douglas Gregor263b47b2010-01-25 16:12:32 +0000849 return false;
850}
851
852bool CursorVisitor::VisitForStmt(ForStmt *S) {
853 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
854 return true;
855 if (VarDecl *Var = S->getConditionVariable()) {
856 if (Visit(MakeCXCursor(Var, TU)))
857 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000858 }
859
Douglas Gregor263b47b2010-01-25 16:12:32 +0000860 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
861 return true;
862 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
863 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000864 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
865 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000866
Douglas Gregorf5bab412010-01-22 01:00:11 +0000867 return false;
868}
869
Douglas Gregor336fd812010-01-23 00:40:08 +0000870bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
871 if (E->isArgumentType()) {
872 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
873 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000874
Douglas Gregor336fd812010-01-23 00:40:08 +0000875 return false;
876 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000877
Douglas Gregor336fd812010-01-23 00:40:08 +0000878 return VisitExpr(E);
879}
880
881bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
882 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
883 if (Visit(TSInfo->getTypeLoc()))
884 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000885
Douglas Gregor336fd812010-01-23 00:40:08 +0000886 return VisitCastExpr(E);
887}
888
889bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
890 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
891 if (Visit(TSInfo->getTypeLoc()))
892 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000893
Douglas Gregor336fd812010-01-23 00:40:08 +0000894 return VisitExpr(E);
895}
896
Ted Kremenek09dfa372010-02-18 05:46:33 +0000897bool CursorVisitor::VisitAttributes(Decl *D) {
898 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
899 if (Visit(MakeCXCursor(A, D, TU)))
900 return true;
901
902 return false;
903}
904
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000905extern "C" {
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000906CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000907 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000908 if (excludeDeclarationsFromPCH)
909 CIdxr->setOnlyLocalDecls();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000910 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000911}
912
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000913void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000914 if (CIdx)
915 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000916}
917
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000918void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000919 if (CIdx) {
920 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
921 CXXIdx->setUseExternalASTGeneration(value);
922 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000923}
924
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000925CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +0000926 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000927 if (!CIdx)
928 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000929
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000930 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000931
Douglas Gregor5352ac02010-01-28 00:27:43 +0000932 // Configure the diagnostics.
933 DiagnosticOptions DiagOpts;
934 llvm::OwningPtr<Diagnostic> Diags;
935 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000936 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000937 CXXIdx->getOnlyLocalDecls(),
938 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000939}
940
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000941CXTranslationUnit
942clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
943 const char *source_filename,
944 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000945 const char **command_line_args,
946 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000947 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000948 if (!CIdx)
949 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000950
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000951 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
952
Douglas Gregor5352ac02010-01-28 00:27:43 +0000953 // Configure the diagnostics.
954 DiagnosticOptions DiagOpts;
955 llvm::OwningPtr<Diagnostic> Diags;
956 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000957
Douglas Gregor4db64a42010-01-23 00:14:00 +0000958 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
959 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000960 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000961 = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
962 unsaved_files[I].Contents + unsaved_files[I].Length,
963 unsaved_files[I].Filename);
964 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
965 Buffer));
966 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000967
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000968 if (!CXXIdx->getUseExternalASTGeneration()) {
969 llvm::SmallVector<const char *, 16> Args;
970
971 // The 'source_filename' argument is optional. If the caller does not
972 // specify it then it is assumed that the source file is specified
973 // in the actual argument list.
974 if (source_filename)
975 Args.push_back(source_filename);
976 Args.insert(Args.end(), command_line_args,
977 command_line_args + num_command_line_args);
978
Douglas Gregor5352ac02010-01-28 00:27:43 +0000979 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000980
Ted Kremenek29b72842010-01-07 22:49:05 +0000981#ifdef USE_CRASHTRACER
982 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000983#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000984
Daniel Dunbar94220972009-12-05 02:17:18 +0000985 llvm::OwningPtr<ASTUnit> Unit(
986 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000987 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000988 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000989 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +0000990 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +0000991 RemappedFiles.size(),
992 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000993
Daniel Dunbar94220972009-12-05 02:17:18 +0000994 // FIXME: Until we have broader testing, just drop the entire AST if we
995 // encountered an error.
Douglas Gregor5352ac02010-01-28 00:27:43 +0000996 if (NumErrors != Diags->getNumErrors())
Daniel Dunbar94220972009-12-05 02:17:18 +0000997 return 0;
998
999 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001000 }
1001
Ted Kremenek139ba862009-10-22 00:03:57 +00001002 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001003 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001004
Ted Kremenek139ba862009-10-22 00:03:57 +00001005 // First add the complete path to the 'clang' executable.
1006 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001007 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001008
Ted Kremenek139ba862009-10-22 00:03:57 +00001009 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001010 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001011
Ted Kremenek139ba862009-10-22 00:03:57 +00001012 // The 'source_filename' argument is optional. If the caller does not
1013 // specify it then it is assumed that the source file is specified
1014 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001015 if (source_filename)
1016 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001017
Steve Naroff37b5ac22009-10-15 20:50:09 +00001018 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001019 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001020 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001021 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001022
Douglas Gregor4db64a42010-01-23 00:14:00 +00001023 // Remap any unsaved files to temporary files.
1024 std::vector<llvm::sys::Path> TemporaryFiles;
1025 std::vector<std::string> RemapArgs;
1026 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1027 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001028
Douglas Gregor4db64a42010-01-23 00:14:00 +00001029 // The pointers into the elements of RemapArgs are stable because we
1030 // won't be adding anything to RemapArgs after this point.
1031 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1032 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001033
Ted Kremenek139ba862009-10-22 00:03:57 +00001034 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1035 for (int i = 0; i < num_command_line_args; ++i)
1036 if (const char *arg = command_line_args[i]) {
1037 if (strcmp(arg, "-o") == 0) {
1038 ++i; // Also skip the matching argument.
1039 continue;
1040 }
1041 if (strcmp(arg, "-emit-ast") == 0 ||
1042 strcmp(arg, "-c") == 0 ||
1043 strcmp(arg, "-fsyntax-only") == 0) {
1044 continue;
1045 }
1046
1047 // Keep the argument.
1048 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001049 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001050
Douglas Gregord93256e2010-01-28 06:00:51 +00001051 // Generate a temporary name for the diagnostics file.
1052 char tmpFileResults[L_tmpnam];
1053 char *tmpResultsFileName = tmpnam(tmpFileResults);
1054 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1055 TemporaryFiles.push_back(DiagnosticsFile);
1056 argv.push_back("-fdiagnostics-binary");
1057
Ted Kremenek139ba862009-10-22 00:03:57 +00001058 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001059 argv.push_back(NULL);
1060
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001061 // Invoke 'clang'.
1062 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1063 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001064 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001065 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1066 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001067 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001068 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001069 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001070
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001071 if (!ErrMsg.empty()) {
1072 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001073 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001074 I != E; ++I) {
1075 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001076 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001077 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001078 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001079
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001080 Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001081 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001082
Douglas Gregor5352ac02010-01-28 00:27:43 +00001083 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001084 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001085 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001086 RemappedFiles.size(),
1087 /*CaptureDiagnostics=*/true);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001088 if (ATU)
1089 ATU->unlinkTemporaryFile();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001090
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001091 // FIXME: Currently we don't report diagnostics on invalid ASTs.
Douglas Gregora88084b2010-02-18 18:08:43 +00001092 if (ATU) {
1093 LoadSerializedDiagnostics(DiagnosticsFile,
1094 num_unsaved_files, unsaved_files,
1095 ATU->getFileManager(),
1096 ATU->getSourceManager(),
1097 ATU->getDiagnostics());
1098 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001099
Douglas Gregor4db64a42010-01-23 00:14:00 +00001100 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1101 TemporaryFiles[i].eraseFromDisk();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001102
Steve Naroffe19944c2009-10-15 22:23:48 +00001103 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001104}
1105
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001106void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001107 if (CTUnit)
1108 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001109}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001110
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001111CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001112 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001113 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001114
Steve Naroff77accc12009-09-03 18:19:54 +00001115 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001116 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001117}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001118
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001119CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001120 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001121 return Result;
1122}
1123
Ted Kremenekfb480492010-01-13 21:46:36 +00001124} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001125
Ted Kremenekfb480492010-01-13 21:46:36 +00001126//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001127// CXSourceLocation and CXSourceRange Operations.
1128//===----------------------------------------------------------------------===//
1129
Douglas Gregorb9790342010-01-22 21:44:22 +00001130extern "C" {
1131CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001132 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001133 return Result;
1134}
1135
1136unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001137 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1138 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1139 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001140}
1141
1142CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1143 CXFile file,
1144 unsigned line,
1145 unsigned column) {
1146 if (!tu)
1147 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001148
Douglas Gregorb9790342010-01-22 21:44:22 +00001149 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1150 SourceLocation SLoc
1151 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001152 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001153 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001154
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001155 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001156}
1157
Douglas Gregor5352ac02010-01-28 00:27:43 +00001158CXSourceRange clang_getNullRange() {
1159 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1160 return Result;
1161}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001162
Douglas Gregor5352ac02010-01-28 00:27:43 +00001163CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1164 if (begin.ptr_data[0] != end.ptr_data[0] ||
1165 begin.ptr_data[1] != end.ptr_data[1])
1166 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001167
1168 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001169 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001170 return Result;
1171}
1172
Douglas Gregor46766dc2010-01-26 19:19:08 +00001173void clang_getInstantiationLocation(CXSourceLocation location,
1174 CXFile *file,
1175 unsigned *line,
1176 unsigned *column,
1177 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001178 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1179
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001180 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001181 if (file)
1182 *file = 0;
1183 if (line)
1184 *line = 0;
1185 if (column)
1186 *column = 0;
1187 if (offset)
1188 *offset = 0;
1189 return;
1190 }
1191
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001192 const SourceManager &SM =
1193 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001194 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001195
1196 if (file)
1197 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1198 if (line)
1199 *line = SM.getInstantiationLineNumber(InstLoc);
1200 if (column)
1201 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001202 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001203 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001204}
1205
Douglas Gregor1db19de2010-01-19 21:36:55 +00001206CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001207 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001208 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001209 return Result;
1210}
1211
1212CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001213 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001214 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001215 return Result;
1216}
1217
Douglas Gregorb9790342010-01-22 21:44:22 +00001218} // end: extern "C"
1219
Douglas Gregor1db19de2010-01-19 21:36:55 +00001220//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001221// CXFile Operations.
1222//===----------------------------------------------------------------------===//
1223
1224extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001225CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001226 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001227 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001228
Steve Naroff88145032009-10-27 14:35:18 +00001229 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001230 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001231}
1232
1233time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001234 if (!SFile)
1235 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001236
Steve Naroff88145032009-10-27 14:35:18 +00001237 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1238 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001239}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001240
Douglas Gregorb9790342010-01-22 21:44:22 +00001241CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1242 if (!tu)
1243 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001244
Douglas Gregorb9790342010-01-22 21:44:22 +00001245 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001246
Douglas Gregorb9790342010-01-22 21:44:22 +00001247 FileManager &FMgr = CXXUnit->getFileManager();
1248 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1249 return const_cast<FileEntry *>(File);
1250}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001251
Ted Kremenekfb480492010-01-13 21:46:36 +00001252} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001253
Ted Kremenekfb480492010-01-13 21:46:36 +00001254//===----------------------------------------------------------------------===//
1255// CXCursor Operations.
1256//===----------------------------------------------------------------------===//
1257
Ted Kremenekfb480492010-01-13 21:46:36 +00001258static Decl *getDeclFromExpr(Stmt *E) {
1259 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1260 return RefExpr->getDecl();
1261 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1262 return ME->getMemberDecl();
1263 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1264 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001265
Ted Kremenekfb480492010-01-13 21:46:36 +00001266 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1267 return getDeclFromExpr(CE->getCallee());
1268 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1269 return getDeclFromExpr(CE->getSubExpr());
1270 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1271 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001272
Ted Kremenekfb480492010-01-13 21:46:36 +00001273 return 0;
1274}
1275
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001276static SourceLocation getLocationFromExpr(Expr *E) {
1277 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1278 return /*FIXME:*/Msg->getLeftLoc();
1279 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1280 return DRE->getLocation();
1281 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1282 return Member->getMemberLoc();
1283 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1284 return Ivar->getLocation();
1285 return E->getLocStart();
1286}
1287
Ted Kremenekfb480492010-01-13 21:46:36 +00001288extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001289
1290unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001291 CXCursorVisitor visitor,
1292 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001293 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001294
1295 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001296
Douglas Gregorb1373d02010-01-20 20:59:29 +00001297 // Set the PCHLevel to filter out unwanted decls if requested.
1298 if (CXXUnit->getOnlyLocalDecls()) {
1299 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001300
Douglas Gregorb1373d02010-01-20 20:59:29 +00001301 // If the main input was an AST, bump the level.
1302 if (CXXUnit->isMainFileAST())
1303 ++PCHLevel;
1304 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001305
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001306 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001307 return CursorVis.VisitChildren(parent);
1308}
1309
Douglas Gregor78205d42010-01-20 21:45:58 +00001310static CXString getDeclSpelling(Decl *D) {
1311 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1312 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001313 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001314
Douglas Gregor78205d42010-01-20 21:45:58 +00001315 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001316 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001317
Douglas Gregor78205d42010-01-20 21:45:58 +00001318 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1319 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1320 // and returns different names. NamedDecl returns the class name and
1321 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001322 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001323
Douglas Gregor78205d42010-01-20 21:45:58 +00001324 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001325 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001326
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001327 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001328}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001329
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001330CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001331 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001332 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001333
Steve Narofff334b4e2009-09-02 18:26:48 +00001334 if (clang_isReference(C.kind)) {
1335 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001336 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001337 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001338 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001339 }
1340 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001341 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001342 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001343 }
1344 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001345 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001346 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001347 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001348 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001349 case CXCursor_TypeRef: {
1350 TypeDecl *Type = getCursorTypeRef(C).first;
1351 assert(Type && "Missing type decl");
1352
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001353 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1354 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001355 }
1356
Daniel Dunbaracca7252009-11-30 20:42:49 +00001357 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001358 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001359 }
1360 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001361
1362 if (clang_isExpression(C.kind)) {
1363 Decl *D = getDeclFromExpr(getCursorExpr(C));
1364 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001365 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001366 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001367 }
1368
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001369 if (clang_isDeclaration(C.kind))
1370 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001371
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001372 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001373}
1374
Ted Kremeneke68fff62010-02-17 00:41:32 +00001375CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001376 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001377 case CXCursor_FunctionDecl:
1378 return createCXString("FunctionDecl");
1379 case CXCursor_TypedefDecl:
1380 return createCXString("TypedefDecl");
1381 case CXCursor_EnumDecl:
1382 return createCXString("EnumDecl");
1383 case CXCursor_EnumConstantDecl:
1384 return createCXString("EnumConstantDecl");
1385 case CXCursor_StructDecl:
1386 return createCXString("StructDecl");
1387 case CXCursor_UnionDecl:
1388 return createCXString("UnionDecl");
1389 case CXCursor_ClassDecl:
1390 return createCXString("ClassDecl");
1391 case CXCursor_FieldDecl:
1392 return createCXString("FieldDecl");
1393 case CXCursor_VarDecl:
1394 return createCXString("VarDecl");
1395 case CXCursor_ParmDecl:
1396 return createCXString("ParmDecl");
1397 case CXCursor_ObjCInterfaceDecl:
1398 return createCXString("ObjCInterfaceDecl");
1399 case CXCursor_ObjCCategoryDecl:
1400 return createCXString("ObjCCategoryDecl");
1401 case CXCursor_ObjCProtocolDecl:
1402 return createCXString("ObjCProtocolDecl");
1403 case CXCursor_ObjCPropertyDecl:
1404 return createCXString("ObjCPropertyDecl");
1405 case CXCursor_ObjCIvarDecl:
1406 return createCXString("ObjCIvarDecl");
1407 case CXCursor_ObjCInstanceMethodDecl:
1408 return createCXString("ObjCInstanceMethodDecl");
1409 case CXCursor_ObjCClassMethodDecl:
1410 return createCXString("ObjCClassMethodDecl");
1411 case CXCursor_ObjCImplementationDecl:
1412 return createCXString("ObjCImplementationDecl");
1413 case CXCursor_ObjCCategoryImplDecl:
1414 return createCXString("ObjCCategoryImplDecl");
1415 case CXCursor_UnexposedDecl:
1416 return createCXString("UnexposedDecl");
1417 case CXCursor_ObjCSuperClassRef:
1418 return createCXString("ObjCSuperClassRef");
1419 case CXCursor_ObjCProtocolRef:
1420 return createCXString("ObjCProtocolRef");
1421 case CXCursor_ObjCClassRef:
1422 return createCXString("ObjCClassRef");
1423 case CXCursor_TypeRef:
1424 return createCXString("TypeRef");
1425 case CXCursor_UnexposedExpr:
1426 return createCXString("UnexposedExpr");
1427 case CXCursor_DeclRefExpr:
1428 return createCXString("DeclRefExpr");
1429 case CXCursor_MemberRefExpr:
1430 return createCXString("MemberRefExpr");
1431 case CXCursor_CallExpr:
1432 return createCXString("CallExpr");
1433 case CXCursor_ObjCMessageExpr:
1434 return createCXString("ObjCMessageExpr");
1435 case CXCursor_UnexposedStmt:
1436 return createCXString("UnexposedStmt");
1437 case CXCursor_InvalidFile:
1438 return createCXString("InvalidFile");
1439 case CXCursor_NoDeclFound:
1440 return createCXString("NoDeclFound");
1441 case CXCursor_NotImplemented:
1442 return createCXString("NotImplemented");
1443 case CXCursor_TranslationUnit:
1444 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001445 case CXCursor_UnexposedAttr:
1446 return createCXString("UnexposedAttr");
1447 case CXCursor_IBActionAttr:
1448 return createCXString("attribute(ibaction)");
1449 case CXCursor_IBOutletAttr:
1450 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001451 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001452
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001453 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001454 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001455}
Steve Naroff89922f82009-08-31 00:59:03 +00001456
Ted Kremeneke68fff62010-02-17 00:41:32 +00001457enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1458 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001459 CXClientData client_data) {
1460 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1461 *BestCursor = cursor;
1462 return CXChildVisit_Recurse;
1463}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001464
Douglas Gregorb9790342010-01-22 21:44:22 +00001465CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1466 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001467 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001468
Douglas Gregorb9790342010-01-22 21:44:22 +00001469 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1470
Ted Kremeneka297de22010-01-25 22:34:44 +00001471 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001472 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1473 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001474 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001475
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001476 // FIXME: Would be great to have a "hint" cursor, then walk from that
1477 // hint cursor upward until we find a cursor whose source range encloses
1478 // the region of interest, rather than starting from the translation unit.
1479 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001480 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001481 Decl::MaxPCHLevel, RegionOfInterest);
1482 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001483 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001484 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001485}
1486
Ted Kremenek73885552009-11-17 19:28:59 +00001487CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001488 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001489}
1490
1491unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001492 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001493}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001494
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001495unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001496 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1497}
1498
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001499unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001500 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1501}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001502
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001503unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001504 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1505}
1506
Douglas Gregor97b98722010-01-19 23:20:36 +00001507unsigned clang_isExpression(enum CXCursorKind K) {
1508 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1509}
1510
1511unsigned clang_isStatement(enum CXCursorKind K) {
1512 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1513}
1514
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001515unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1516 return K == CXCursor_TranslationUnit;
1517}
1518
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001519CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001520 return C.kind;
1521}
1522
Douglas Gregor98258af2010-01-18 22:46:11 +00001523CXSourceLocation clang_getCursorLocation(CXCursor C) {
1524 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001525 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001526 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001527 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1528 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001529 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001530 }
1531
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001532 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001533 std::pair<ObjCProtocolDecl *, SourceLocation> P
1534 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001535 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001536 }
1537
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001538 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001539 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1540 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001541 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001542 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001543
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001544 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001545 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001546 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001547 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001548
Douglas Gregorf46034a2010-01-18 23:41:10 +00001549 default:
1550 // FIXME: Need a way to enumerate all non-reference cases.
1551 llvm_unreachable("Missed a reference kind");
1552 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001553 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001554
1555 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001556 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001557 getLocationFromExpr(getCursorExpr(C)));
1558
Douglas Gregor5352ac02010-01-28 00:27:43 +00001559 if (!getCursorDecl(C))
1560 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001561
Douglas Gregorf46034a2010-01-18 23:41:10 +00001562 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001563 SourceLocation Loc = D->getLocation();
1564 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1565 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001566 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001567}
Douglas Gregora7bde202010-01-19 00:34:46 +00001568
1569CXSourceRange clang_getCursorExtent(CXCursor C) {
1570 if (clang_isReference(C.kind)) {
1571 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001572 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001573 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1574 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001575 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001576 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001577
1578 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001579 std::pair<ObjCProtocolDecl *, SourceLocation> P
1580 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001581 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001582 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001583
1584 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001585 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1586 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001587
Ted Kremeneka297de22010-01-25 22:34:44 +00001588 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001589 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001590
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001591 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001592 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001593 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001594 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001595
Douglas Gregora7bde202010-01-19 00:34:46 +00001596 default:
1597 // FIXME: Need a way to enumerate all non-reference cases.
1598 llvm_unreachable("Missed a reference kind");
1599 }
1600 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001601
1602 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001603 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001604 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001605
1606 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001607 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001608 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001609
Douglas Gregor5352ac02010-01-28 00:27:43 +00001610 if (!getCursorDecl(C))
1611 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001612
Douglas Gregora7bde202010-01-19 00:34:46 +00001613 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001614 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001615}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001616
1617CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001618 if (clang_isInvalid(C.kind))
1619 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001620
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001621 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001622 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001623 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001624
Douglas Gregor97b98722010-01-19 23:20:36 +00001625 if (clang_isExpression(C.kind)) {
1626 Decl *D = getDeclFromExpr(getCursorExpr(C));
1627 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001628 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001629 return clang_getNullCursor();
1630 }
1631
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001632 if (!clang_isReference(C.kind))
1633 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001634
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001635 switch (C.kind) {
1636 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001637 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001638
1639 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001640 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001641
1642 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001643 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001644
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001645 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001646 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001647
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001648 default:
1649 // We would prefer to enumerate all non-reference cursor kinds here.
1650 llvm_unreachable("Unhandled reference cursor kind");
1651 break;
1652 }
1653 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001654
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001655 return clang_getNullCursor();
1656}
1657
Douglas Gregorb6998662010-01-19 19:34:47 +00001658CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001659 if (clang_isInvalid(C.kind))
1660 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001661
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001662 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001663
Douglas Gregorb6998662010-01-19 19:34:47 +00001664 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001665 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001666 C = clang_getCursorReferenced(C);
1667 WasReference = true;
1668 }
1669
1670 if (!clang_isDeclaration(C.kind))
1671 return clang_getNullCursor();
1672
1673 Decl *D = getCursorDecl(C);
1674 if (!D)
1675 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001676
Douglas Gregorb6998662010-01-19 19:34:47 +00001677 switch (D->getKind()) {
1678 // Declaration kinds that don't really separate the notions of
1679 // declaration and definition.
1680 case Decl::Namespace:
1681 case Decl::Typedef:
1682 case Decl::TemplateTypeParm:
1683 case Decl::EnumConstant:
1684 case Decl::Field:
1685 case Decl::ObjCIvar:
1686 case Decl::ObjCAtDefsField:
1687 case Decl::ImplicitParam:
1688 case Decl::ParmVar:
1689 case Decl::NonTypeTemplateParm:
1690 case Decl::TemplateTemplateParm:
1691 case Decl::ObjCCategoryImpl:
1692 case Decl::ObjCImplementation:
1693 case Decl::LinkageSpec:
1694 case Decl::ObjCPropertyImpl:
1695 case Decl::FileScopeAsm:
1696 case Decl::StaticAssert:
1697 case Decl::Block:
1698 return C;
1699
1700 // Declaration kinds that don't make any sense here, but are
1701 // nonetheless harmless.
1702 case Decl::TranslationUnit:
1703 case Decl::Template:
1704 case Decl::ObjCContainer:
1705 break;
1706
1707 // Declaration kinds for which the definition is not resolvable.
1708 case Decl::UnresolvedUsingTypename:
1709 case Decl::UnresolvedUsingValue:
1710 break;
1711
1712 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001713 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1714 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001715
1716 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001717 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001718
1719 case Decl::Enum:
1720 case Decl::Record:
1721 case Decl::CXXRecord:
1722 case Decl::ClassTemplateSpecialization:
1723 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001724 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001725 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001726 return clang_getNullCursor();
1727
1728 case Decl::Function:
1729 case Decl::CXXMethod:
1730 case Decl::CXXConstructor:
1731 case Decl::CXXDestructor:
1732 case Decl::CXXConversion: {
1733 const FunctionDecl *Def = 0;
1734 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001735 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001736 return clang_getNullCursor();
1737 }
1738
1739 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001740 // Ask the variable if it has a definition.
1741 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1742 return MakeCXCursor(Def, CXXUnit);
1743 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001744 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001745
Douglas Gregorb6998662010-01-19 19:34:47 +00001746 case Decl::FunctionTemplate: {
1747 const FunctionDecl *Def = 0;
1748 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001749 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001750 return clang_getNullCursor();
1751 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001752
Douglas Gregorb6998662010-01-19 19:34:47 +00001753 case Decl::ClassTemplate: {
1754 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001755 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001756 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001757 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001758 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001759 return clang_getNullCursor();
1760 }
1761
1762 case Decl::Using: {
1763 UsingDecl *Using = cast<UsingDecl>(D);
1764 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001765 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1766 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001767 S != SEnd; ++S) {
1768 if (Def != clang_getNullCursor()) {
1769 // FIXME: We have no way to return multiple results.
1770 return clang_getNullCursor();
1771 }
1772
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001773 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001774 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001775 }
1776
1777 return Def;
1778 }
1779
1780 case Decl::UsingShadow:
1781 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001782 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001783 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001784
1785 case Decl::ObjCMethod: {
1786 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1787 if (Method->isThisDeclarationADefinition())
1788 return C;
1789
1790 // Dig out the method definition in the associated
1791 // @implementation, if we have it.
1792 // FIXME: The ASTs should make finding the definition easier.
1793 if (ObjCInterfaceDecl *Class
1794 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1795 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1796 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1797 Method->isInstanceMethod()))
1798 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001799 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001800
1801 return clang_getNullCursor();
1802 }
1803
1804 case Decl::ObjCCategory:
1805 if (ObjCCategoryImplDecl *Impl
1806 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001807 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001808 return clang_getNullCursor();
1809
1810 case Decl::ObjCProtocol:
1811 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1812 return C;
1813 return clang_getNullCursor();
1814
1815 case Decl::ObjCInterface:
1816 // There are two notions of a "definition" for an Objective-C
1817 // class: the interface and its implementation. When we resolved a
1818 // reference to an Objective-C class, produce the @interface as
1819 // the definition; when we were provided with the interface,
1820 // produce the @implementation as the definition.
1821 if (WasReference) {
1822 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1823 return C;
1824 } else if (ObjCImplementationDecl *Impl
1825 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001826 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001827 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001828
Douglas Gregorb6998662010-01-19 19:34:47 +00001829 case Decl::ObjCProperty:
1830 // FIXME: We don't really know where to find the
1831 // ObjCPropertyImplDecls that implement this property.
1832 return clang_getNullCursor();
1833
1834 case Decl::ObjCCompatibleAlias:
1835 if (ObjCInterfaceDecl *Class
1836 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1837 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001838 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001839
Douglas Gregorb6998662010-01-19 19:34:47 +00001840 return clang_getNullCursor();
1841
1842 case Decl::ObjCForwardProtocol: {
1843 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1844 if (Forward->protocol_size() == 1)
1845 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001846 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001847 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001848
1849 // FIXME: Cannot return multiple definitions.
1850 return clang_getNullCursor();
1851 }
1852
1853 case Decl::ObjCClass: {
1854 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1855 if (Class->size() == 1) {
1856 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1857 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001858 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001859 return clang_getNullCursor();
1860 }
1861
1862 // FIXME: Cannot return multiple definitions.
1863 return clang_getNullCursor();
1864 }
1865
1866 case Decl::Friend:
1867 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001868 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001869 return clang_getNullCursor();
1870
1871 case Decl::FriendTemplate:
1872 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001873 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001874 return clang_getNullCursor();
1875 }
1876
1877 return clang_getNullCursor();
1878}
1879
1880unsigned clang_isCursorDefinition(CXCursor C) {
1881 if (!clang_isDeclaration(C.kind))
1882 return 0;
1883
1884 return clang_getCursorDefinition(C) == C;
1885}
1886
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001887void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001888 const char **startBuf,
1889 const char **endBuf,
1890 unsigned *startLine,
1891 unsigned *startColumn,
1892 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001893 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001894 assert(getCursorDecl(C) && "CXCursor has null decl");
1895 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001896 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1897 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001898
Steve Naroff4ade6d62009-09-23 17:52:52 +00001899 SourceManager &SM = FD->getASTContext().getSourceManager();
1900 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1901 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1902 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1903 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1904 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1905 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1906}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001907
Ted Kremenekfb480492010-01-13 21:46:36 +00001908} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001909
Ted Kremenekfb480492010-01-13 21:46:36 +00001910//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001911// Token-based Operations.
1912//===----------------------------------------------------------------------===//
1913
1914/* CXToken layout:
1915 * int_data[0]: a CXTokenKind
1916 * int_data[1]: starting token location
1917 * int_data[2]: token length
1918 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001919 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001920 * otherwise unused.
1921 */
1922extern "C" {
1923
1924CXTokenKind clang_getTokenKind(CXToken CXTok) {
1925 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1926}
1927
1928CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1929 switch (clang_getTokenKind(CXTok)) {
1930 case CXToken_Identifier:
1931 case CXToken_Keyword:
1932 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001933 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1934 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001935
1936 case CXToken_Literal: {
1937 // We have stashed the starting pointer in the ptr_data field. Use it.
1938 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001939 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001940 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001941
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001942 case CXToken_Punctuation:
1943 case CXToken_Comment:
1944 break;
1945 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001946
1947 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001948 // deconstructing the source location.
1949 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1950 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001951 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001952
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001953 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
1954 std::pair<FileID, unsigned> LocInfo
1955 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
1956 std::pair<const char *,const char *> Buffer
1957 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
1958
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001959 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
1960 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001961}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001962
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001963CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
1964 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1965 if (!CXXUnit)
1966 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001967
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001968 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
1969 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1970}
1971
1972CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
1973 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00001974 if (!CXXUnit)
1975 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001976
1977 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001978 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1979}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001980
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001981void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1982 CXToken **Tokens, unsigned *NumTokens) {
1983 if (Tokens)
1984 *Tokens = 0;
1985 if (NumTokens)
1986 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001987
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001988 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1989 if (!CXXUnit || !Tokens || !NumTokens)
1990 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001991
Daniel Dunbar85b988f2010-02-14 08:31:57 +00001992 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001993 if (R.isInvalid())
1994 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001995
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001996 SourceManager &SourceMgr = CXXUnit->getSourceManager();
1997 std::pair<FileID, unsigned> BeginLocInfo
1998 = SourceMgr.getDecomposedLoc(R.getBegin());
1999 std::pair<FileID, unsigned> EndLocInfo
2000 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002001
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002002 // Cannot tokenize across files.
2003 if (BeginLocInfo.first != EndLocInfo.first)
2004 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002005
2006 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002007 std::pair<const char *,const char *> Buffer
2008 = SourceMgr.getBufferData(BeginLocInfo.first);
2009 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2010 CXXUnit->getASTContext().getLangOptions(),
2011 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2012 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002013
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002014 // Lex tokens until we hit the end of the range.
2015 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2016 llvm::SmallVector<CXToken, 32> CXTokens;
2017 Token Tok;
2018 do {
2019 // Lex the next token
2020 Lex.LexFromRawLexer(Tok);
2021 if (Tok.is(tok::eof))
2022 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002023
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002024 // Initialize the CXToken.
2025 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002026
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002027 // - Common fields
2028 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2029 CXTok.int_data[2] = Tok.getLength();
2030 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002031
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002032 // - Kind-specific fields
2033 if (Tok.isLiteral()) {
2034 CXTok.int_data[0] = CXToken_Literal;
2035 CXTok.ptr_data = (void *)Tok.getLiteralData();
2036 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002037 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002038 std::pair<FileID, unsigned> LocInfo
2039 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002040 const char *StartPos
2041 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002042 LocInfo.second;
2043 IdentifierInfo *II
2044 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2045 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2046 CXToken_Identifier
2047 : CXToken_Keyword;
2048 CXTok.ptr_data = II;
2049 } else if (Tok.is(tok::comment)) {
2050 CXTok.int_data[0] = CXToken_Comment;
2051 CXTok.ptr_data = 0;
2052 } else {
2053 CXTok.int_data[0] = CXToken_Punctuation;
2054 CXTok.ptr_data = 0;
2055 }
2056 CXTokens.push_back(CXTok);
2057 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002058
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002059 if (CXTokens.empty())
2060 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002061
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002062 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2063 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2064 *NumTokens = CXTokens.size();
2065}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002066
2067typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2068
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002069enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2070 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002071 CXClientData client_data) {
2072 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2073
2074 // We only annotate the locations of declarations, simple
2075 // references, and expressions which directly reference something.
2076 CXCursorKind Kind = clang_getCursorKind(cursor);
2077 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2078 // Okay: We can annotate the location of this declaration with the
2079 // declaration or reference
2080 } else if (clang_isExpression(cursor.kind)) {
2081 if (Kind != CXCursor_DeclRefExpr &&
2082 Kind != CXCursor_MemberRefExpr &&
2083 Kind != CXCursor_ObjCMessageExpr)
2084 return CXChildVisit_Recurse;
2085
2086 CXCursor Referenced = clang_getCursorReferenced(cursor);
2087 if (Referenced == cursor || Referenced == clang_getNullCursor())
2088 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002089
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002090 // Okay: we can annotate the location of this expression
2091 } else {
2092 // Nothing to annotate
2093 return CXChildVisit_Recurse;
2094 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002095
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002096 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2097 (*Data)[Loc.int_data] = cursor;
2098 return CXChildVisit_Recurse;
2099}
2100
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002101void clang_annotateTokens(CXTranslationUnit TU,
2102 CXToken *Tokens, unsigned NumTokens,
2103 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002104 if (NumTokens == 0)
2105 return;
2106
2107 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002108 for (unsigned I = 0; I != NumTokens; ++I)
2109 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002110
2111 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2112 if (!CXXUnit || !Tokens)
2113 return;
2114
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002115 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002116 SourceRange RegionOfInterest;
2117 RegionOfInterest.setBegin(
2118 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2119 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002120 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002121 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002122 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002123 // FIXME: Would be great to have a "hint" cursor, then walk from that
2124 // hint cursor upward until we find a cursor whose source range encloses
2125 // the region of interest, rather than starting from the translation unit.
2126 AnnotateTokensData Annotated;
2127 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002128 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002129 Decl::MaxPCHLevel, RegionOfInterest);
2130 AnnotateVis.VisitChildren(Parent);
2131
2132 for (unsigned I = 0; I != NumTokens; ++I) {
2133 // Determine whether we saw a cursor at this token's location.
2134 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2135 if (Pos == Annotated.end())
2136 continue;
2137
2138 Cursors[I] = Pos->second;
2139 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002140}
2141
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002142void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002143 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002144 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002145}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002146
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002147} // end: extern "C"
2148
2149//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002150// CXString Operations.
2151//===----------------------------------------------------------------------===//
2152
2153extern "C" {
2154const char *clang_getCString(CXString string) {
2155 return string.Spelling;
2156}
2157
2158void clang_disposeString(CXString string) {
2159 if (string.MustFreeString && string.Spelling)
2160 free((void*)string.Spelling);
2161}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002162
Ted Kremenekfb480492010-01-13 21:46:36 +00002163} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002164
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002165namespace clang { namespace cxstring {
2166CXString createCXString(const char *String, bool DupString){
2167 CXString Str;
2168 if (DupString) {
2169 Str.Spelling = strdup(String);
2170 Str.MustFreeString = 1;
2171 } else {
2172 Str.Spelling = String;
2173 Str.MustFreeString = 0;
2174 }
2175 return Str;
2176}
2177
2178CXString createCXString(llvm::StringRef String, bool DupString) {
2179 CXString Result;
2180 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2181 char *Spelling = (char *)malloc(String.size() + 1);
2182 memmove(Spelling, String.data(), String.size());
2183 Spelling[String.size()] = 0;
2184 Result.Spelling = Spelling;
2185 Result.MustFreeString = 1;
2186 } else {
2187 Result.Spelling = String.data();
2188 Result.MustFreeString = 0;
2189 }
2190 return Result;
2191}
2192}}
2193
Ted Kremenek04bb7162010-01-22 22:44:15 +00002194//===----------------------------------------------------------------------===//
2195// Misc. utility functions.
2196//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002197
Ted Kremenek04bb7162010-01-22 22:44:15 +00002198extern "C" {
2199
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002200CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002201 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002202}
2203
2204} // end: extern "C"