blob: 6221a615aa713f8cc7080628588cac63d058fc98 [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"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000025#include "clang/Basic/Diagnostic.h"
26#include "clang/Frontend/ASTUnit.h"
27#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000029#include "clang/Lex/Lexer.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000031#include "clang/Lex/Preprocessor.h"
Douglas Gregor02465752009-10-16 21:24:31 +000032#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000033#include "llvm/System/Program.h"
Douglas Gregor0a812cf2010-02-18 23:07:20 +000034#include "llvm/System/Signals.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000035
Benjamin Kramerc2a98162010-03-13 21:22:49 +000036// Needed to define L_TMPNAM on some systems.
37#include <cstdio>
38
Steve Naroff50398192009-08-28 15:28:48 +000039using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000040using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000041using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000042
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000043//===----------------------------------------------------------------------===//
44// Crash Reporting.
45//===----------------------------------------------------------------------===//
46
47#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000048#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000049#include "clang/Analysis/Support/SaveAndRestore.h"
50// Integrate with crash reporter.
51extern "C" const char *__crashreporter_info__;
Ted Kremenek6b569992010-02-17 21:12:23 +000052#define NUM_CRASH_STRINGS 32
Ted Kremenek29b72842010-01-07 22:49:05 +000053static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000054static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000055static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
56static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
57
58static unsigned SetCrashTracerInfo(const char *str,
59 llvm::SmallString<1024> &AggStr) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000060
Ted Kremenek254ba7c2010-01-07 23:13:53 +000061 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000062 while (crashtracer_strings[slot]) {
63 if (++slot == NUM_CRASH_STRINGS)
64 slot = 0;
65 }
66 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000067 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000068
69 // We need to create an aggregate string because multiple threads
70 // may be in this method at one time. The crash reporter string
71 // will attempt to overapproximate the set of in-flight invocations
72 // of this function. Race conditions can still cause this goal
73 // to not be achieved.
74 {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000075 llvm::raw_svector_ostream Out(AggStr);
Ted Kremenek29b72842010-01-07 22:49:05 +000076 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
77 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
78 }
79 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
80 return slot;
81}
82
83static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000084 unsigned max_slot = 0;
85 unsigned max_value = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +000086
Ted Kremenek254ba7c2010-01-07 23:13:53 +000087 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
88
89 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
90 if (agg_crashtracer_strings[i] &&
91 crashtracer_counter_id[i] > max_value) {
92 max_slot = i;
93 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000094 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000095
96 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000097}
98
99namespace {
100class ArgsCrashTracerInfo {
101 llvm::SmallString<1024> CrashString;
102 llvm::SmallString<1024> AggregateString;
103 unsigned crashtracerSlot;
104public:
105 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
106 : crashtracerSlot(0)
107 {
108 {
109 llvm::raw_svector_ostream Out(CrashString);
Ted Kremenek0baa9522010-03-05 22:43:25 +0000110 Out << "ClangCIndex [" << getClangFullVersion() << "]"
111 << "[createTranslationUnitFromSourceFile]: clang";
Ted Kremenek29b72842010-01-07 22:49:05 +0000112 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
113 E=Args.end(); I!=E; ++I)
114 Out << ' ' << *I;
115 }
116 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
117 AggregateString);
118 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000119
Ted Kremenek29b72842010-01-07 22:49:05 +0000120 ~ArgsCrashTracerInfo() {
121 ResetCrashTracerInfo(crashtracerSlot);
122 }
123};
124}
125#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000126
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000127/// \brief The result of comparing two source ranges.
128enum RangeComparisonResult {
129 /// \brief Either the ranges overlap or one of the ranges is invalid.
130 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000131
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000132 /// \brief The first range ends before the second range starts.
133 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000134
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000135 /// \brief The first range starts after the second range ends.
136 RangeAfter
137};
138
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000139/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000140/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000141static RangeComparisonResult RangeCompare(SourceManager &SM,
142 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000143 SourceRange R2) {
144 assert(R1.isValid() && "First range is invalid?");
145 assert(R2.isValid() && "Second range is invalid?");
Daniel Dunbard52864b2010-02-14 10:02:57 +0000146 if (R1.getEnd() == R2.getBegin() ||
147 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000148 return RangeBefore;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000149 if (R2.getEnd() == R1.getBegin() ||
150 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000151 return RangeAfter;
152 return RangeOverlap;
153}
154
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000155/// \brief Translate a Clang source range into a CIndex source range.
156///
157/// Clang internally represents ranges where the end location points to the
158/// start of the token at the end. However, for external clients it is more
159/// useful to have a CXSourceRange be a proper half-open interval. This routine
160/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000161CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000162 const LangOptions &LangOpts,
163 SourceRange R) {
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000164 // We want the last character in this location, so we will adjust the
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000165 // location accordingly.
166 // FIXME: How do do this with a macro instantiation location?
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000167 SourceLocation EndLoc = R.getEnd();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000168 if (!EndLoc.isInvalid() && EndLoc.isFileID()) {
169 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000170 EndLoc = EndLoc.getFileLocWithOffset(Length);
171 }
172
173 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
174 R.getBegin().getRawEncoding(),
175 EndLoc.getRawEncoding() };
176 return Result;
177}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000178
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000179//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000180// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000181//===----------------------------------------------------------------------===//
182
Steve Naroff89922f82009-08-31 00:59:03 +0000183namespace {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000184
Douglas Gregorb1373d02010-01-20 20:59:29 +0000185// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000186class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregora59e3902010-01-21 23:27:09 +0000187 public TypeLocVisitor<CursorVisitor, bool>,
188 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000189{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000190 /// \brief The translation unit we are traversing.
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000191 ASTUnit *TU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000192
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000193 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000194 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000195
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000196 /// \brief The declaration that serves at the parent of any statement or
197 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000198 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000199
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000200 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000201 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000202
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000203 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000204 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000205
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000206 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
207 // to the visitor. Declarations with a PCH level greater than this value will
208 // be suppressed.
209 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000210
211 /// \brief When valid, a source range to which the cursor should restrict
212 /// its search.
213 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000214
Douglas Gregorb1373d02010-01-20 20:59:29 +0000215 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000216 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregora59e3902010-01-21 23:27:09 +0000217 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000218
219 /// \brief Determine whether this particular source range comes before, comes
220 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000221 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000222 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000223 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
224
Steve Naroff89922f82009-08-31 00:59:03 +0000225public:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000226 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
227 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000228 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000229 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000230 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000231 {
232 Parent.kind = CXCursor_NoDeclFound;
233 Parent.data[0] = 0;
234 Parent.data[1] = 0;
235 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000236 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000237 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000238
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000239 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000240
241 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
242 getPreprocessedEntities();
243
Douglas Gregorb1373d02010-01-20 20:59:29 +0000244 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000245
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000246 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000247 bool VisitAttributes(Decl *D);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000248 bool VisitBlockDecl(BlockDecl *B);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000249 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000250 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
251 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000252 bool VisitTagDecl(TagDecl *D);
253 bool VisitEnumConstantDecl(EnumConstantDecl *D);
254 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
255 bool VisitFunctionDecl(FunctionDecl *ND);
256 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000257 bool VisitVarDecl(VarDecl *);
Ted Kremenek79758f62010-02-18 22:36:18 +0000258 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
259 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
260 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
261 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
262 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
263 bool VisitObjCImplDecl(ObjCImplDecl *D);
264 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
265 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
266 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
267 // etc.
268 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
269 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
270 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000271
272 // Type visitors
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000273 // FIXME: QualifiedTypeLoc doesn't provide any location information
274 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000275 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000276 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
277 bool VisitTagTypeLoc(TagTypeLoc TL);
278 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
279 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
280 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
281 bool VisitPointerTypeLoc(PointerTypeLoc TL);
282 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
283 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
284 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
285 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
286 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
287 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000288 // FIXME: Implement for TemplateSpecializationTypeLoc
289 // FIXME: Implement visitors here when the unimplemented TypeLocs get
290 // implemented
291 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
292 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000293
Douglas Gregora59e3902010-01-21 23:27:09 +0000294 // Statement visitors
295 bool VisitStmt(Stmt *S);
296 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000297 // FIXME: LabelStmt label?
298 bool VisitIfStmt(IfStmt *S);
299 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000300 bool VisitWhileStmt(WhileStmt *S);
301 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000302
Douglas Gregor336fd812010-01-23 00:40:08 +0000303 // Expression visitors
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000304 bool VisitBlockExpr(BlockExpr *B);
Douglas Gregor336fd812010-01-23 00:40:08 +0000305 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000306 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
Douglas Gregorc2350e52010-03-08 16:40:19 +0000307 bool VisitObjCMessageExpr(ObjCMessageExpr *E);
Douglas Gregor81d34662010-04-20 15:39:42 +0000308 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000309 bool VisitOffsetOfExpr(OffsetOfExpr *E);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000310 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000311};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000312
Ted Kremenekab188932010-01-05 19:32:54 +0000313} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000314
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000315RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000316 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
317}
318
Douglas Gregorb1373d02010-01-20 20:59:29 +0000319/// \brief Visit the given cursor and, if requested by the visitor,
320/// its children.
321///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000322/// \param Cursor the cursor to visit.
323///
324/// \param CheckRegionOfInterest if true, then the caller already checked that
325/// this cursor is within the region of interest.
326///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000327/// \returns true if the visitation should be aborted, false if it
328/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000329bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000330 if (clang_isInvalid(Cursor.kind))
331 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000332
Douglas Gregorb1373d02010-01-20 20:59:29 +0000333 if (clang_isDeclaration(Cursor.kind)) {
334 Decl *D = getCursorDecl(Cursor);
335 assert(D && "Invalid declaration cursor");
336 if (D->getPCHLevel() > MaxPCHLevel)
337 return false;
338
339 if (D->isImplicit())
340 return false;
341 }
342
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000343 // If we have a range of interest, and this cursor doesn't intersect with it,
344 // we're done.
345 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000346 SourceRange Range =
347 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
348 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000349 return false;
350 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000351
Douglas Gregorb1373d02010-01-20 20:59:29 +0000352 switch (Visitor(Cursor, Parent, ClientData)) {
353 case CXChildVisit_Break:
354 return true;
355
356 case CXChildVisit_Continue:
357 return false;
358
359 case CXChildVisit_Recurse:
360 return VisitChildren(Cursor);
361 }
362
Douglas Gregorfd643772010-01-25 16:45:46 +0000363 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000364}
365
Douglas Gregor788f5a12010-03-20 00:41:21 +0000366std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
367CursorVisitor::getPreprocessedEntities() {
368 PreprocessingRecord &PPRec
369 = *TU->getPreprocessor().getPreprocessingRecord();
370
371 bool OnlyLocalDecls
372 = !TU->isMainFileAST() && TU->getOnlyLocalDecls();
373
374 // There is no region of interest; we have to walk everything.
375 if (RegionOfInterest.isInvalid())
376 return std::make_pair(PPRec.begin(OnlyLocalDecls),
377 PPRec.end(OnlyLocalDecls));
378
379 // Find the file in which the region of interest lands.
380 SourceManager &SM = TU->getSourceManager();
381 std::pair<FileID, unsigned> Begin
382 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
383 std::pair<FileID, unsigned> End
384 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
385
386 // The region of interest spans files; we have to walk everything.
387 if (Begin.first != End.first)
388 return std::make_pair(PPRec.begin(OnlyLocalDecls),
389 PPRec.end(OnlyLocalDecls));
390
391 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
392 = TU->getPreprocessedEntitiesByFile();
393 if (ByFileMap.empty()) {
394 // Build the mapping from files to sets of preprocessed entities.
395 for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls),
396 EEnd = PPRec.end(OnlyLocalDecls);
397 E != EEnd; ++E) {
398 std::pair<FileID, unsigned> P
399 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
400 ByFileMap[P.first].push_back(*E);
401 }
402 }
403
404 return std::make_pair(ByFileMap[Begin.first].begin(),
405 ByFileMap[Begin.first].end());
406}
407
Douglas Gregorb1373d02010-01-20 20:59:29 +0000408/// \brief Visit the children of the given cursor.
409///
410/// \returns true if the visitation should be aborted, false if it
411/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000412bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000413 if (clang_isReference(Cursor.kind)) {
414 // By definition, references have no children.
415 return false;
416 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000417
418 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000419 // done.
420 class SetParentRAII {
421 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000422 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000423 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000424
Douglas Gregorb1373d02010-01-20 20:59:29 +0000425 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000426 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000427 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000428 {
429 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000430 if (clang_isDeclaration(Parent.kind))
431 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000432 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000433
Douglas Gregorb1373d02010-01-20 20:59:29 +0000434 ~SetParentRAII() {
435 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000436 if (clang_isDeclaration(Parent.kind))
437 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000438 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000439 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000440
Douglas Gregorb1373d02010-01-20 20:59:29 +0000441 if (clang_isDeclaration(Cursor.kind)) {
442 Decl *D = getCursorDecl(Cursor);
443 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000444 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000445 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000446
Douglas Gregora59e3902010-01-21 23:27:09 +0000447 if (clang_isStatement(Cursor.kind))
448 return Visit(getCursorStmt(Cursor));
449 if (clang_isExpression(Cursor.kind))
450 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000451
Douglas Gregorb1373d02010-01-20 20:59:29 +0000452 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000453 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000454 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
455 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000456 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
457 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
458 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000459 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000460 return true;
461 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000462 } else if (VisitDeclContext(
463 CXXUnit->getASTContext().getTranslationUnitDecl()))
464 return true;
Bob Wilson3178cb62010-03-19 03:57:57 +0000465
Douglas Gregor0396f462010-03-19 05:22:59 +0000466 // Walk the preprocessing record.
Daniel Dunbar8de30ff2010-03-20 01:11:56 +0000467 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000468 // FIXME: Once we have the ability to deserialize a preprocessing record,
469 // do so.
Douglas Gregor788f5a12010-03-20 00:41:21 +0000470 PreprocessingRecord::iterator E, EEnd;
471 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000472 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
473 if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit)))
474 return true;
Douglas Gregor788f5a12010-03-20 00:41:21 +0000475
Douglas Gregor0396f462010-03-19 05:22:59 +0000476 continue;
477 }
478
479 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
480 if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit)))
481 return true;
482
483 continue;
484 }
485 }
486 }
Douglas Gregor7b691f332010-01-20 21:13:59 +0000487 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000488 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000489
Douglas Gregorb1373d02010-01-20 20:59:29 +0000490 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000491 return false;
492}
493
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000494bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
495 for (BlockDecl::param_iterator I=B->param_begin(), E=B->param_end(); I!=E;++I)
496 if (Decl *D = *I)
497 if (Visit(D))
498 return true;
499
500 return Visit(MakeCXCursor(B->getBody(), StmtParent, TU));
501}
502
Douglas Gregorb1373d02010-01-20 20:59:29 +0000503bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000504 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000505 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek09dfa372010-02-18 05:46:33 +0000506
Daniel Dunbard52864b2010-02-14 10:02:57 +0000507 CXCursor Cursor = MakeCXCursor(*I, TU);
508
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000509 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000510 SourceRange Range =
511 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
512 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000513 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000514
515 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000516 case RangeBefore:
517 // This declaration comes before the region of interest; skip it.
518 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000519
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000520 case RangeAfter:
521 // This declaration comes after the region of interest; we're done.
522 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000523
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000524 case RangeOverlap:
525 // This declaration overlaps the region of interest; visit it.
526 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000527 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000528 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000529
Daniel Dunbard52864b2010-02-14 10:02:57 +0000530 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000531 return true;
532 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000533
Douglas Gregorb1373d02010-01-20 20:59:29 +0000534 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000535}
536
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000537bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
538 llvm_unreachable("Translation units are visited directly by Visit()");
539 return false;
540}
541
542bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
543 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
544 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000545
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000546 return false;
547}
548
549bool CursorVisitor::VisitTagDecl(TagDecl *D) {
550 return VisitDeclContext(D);
551}
552
553bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
554 if (Expr *Init = D->getInitExpr())
555 return Visit(MakeCXCursor(Init, StmtParent, TU));
556 return false;
557}
558
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000559bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
560 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
561 if (Visit(TSInfo->getTypeLoc()))
562 return true;
563
564 return false;
565}
566
Douglas Gregorb1373d02010-01-20 20:59:29 +0000567bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000568 if (VisitDeclaratorDecl(ND))
569 return true;
570
Douglas Gregora59e3902010-01-21 23:27:09 +0000571 if (ND->isThisDeclarationADefinition() &&
572 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
573 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000574
Douglas Gregorb1373d02010-01-20 20:59:29 +0000575 return false;
576}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000577
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000578bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
579 if (VisitDeclaratorDecl(D))
580 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000581
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000582 if (Expr *BitWidth = D->getBitWidth())
583 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000584
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000585 return false;
586}
587
588bool CursorVisitor::VisitVarDecl(VarDecl *D) {
589 if (VisitDeclaratorDecl(D))
590 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000591
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000592 if (Expr *Init = D->getInit())
593 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000594
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000595 return false;
596}
597
598bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000599 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
600 if (Visit(TSInfo->getTypeLoc()))
601 return true;
602
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000603 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000604 PEnd = ND->param_end();
605 P != PEnd; ++P) {
606 if (Visit(MakeCXCursor(*P, TU)))
607 return true;
608 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000609
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000610 if (ND->isThisDeclarationADefinition() &&
611 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
612 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000613
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000614 return false;
615}
616
Douglas Gregora59e3902010-01-21 23:27:09 +0000617bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
618 return VisitDeclContext(D);
619}
620
Douglas Gregorb1373d02010-01-20 20:59:29 +0000621bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000622 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
623 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000624 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000625
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000626 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
627 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
628 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000629 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000630 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000631
Douglas Gregora59e3902010-01-21 23:27:09 +0000632 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000633}
634
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000635bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
636 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
637 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
638 E = PID->protocol_end(); I != E; ++I, ++PL)
639 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
640 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000641
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000642 return VisitObjCContainerDecl(PID);
643}
644
Douglas Gregorb1373d02010-01-20 20:59:29 +0000645bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000646 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000647 if (D->getSuperClass() &&
648 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000649 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000650 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000651 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000652
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000653 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
654 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
655 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000656 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000657 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000658
Douglas Gregora59e3902010-01-21 23:27:09 +0000659 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000660}
661
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000662bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
663 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000664}
665
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000666bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000667 // 'ID' could be null when dealing with invalid code.
668 if (ObjCInterfaceDecl *ID = D->getClassInterface())
669 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
670 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000671
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000672 return VisitObjCImplDecl(D);
673}
674
675bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
676#if 0
677 // Issue callbacks for super class.
678 // FIXME: No source location information!
679 if (D->getSuperClass() &&
680 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000681 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000682 TU)))
683 return true;
684#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000685
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000686 return VisitObjCImplDecl(D);
687}
688
689bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
690 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
691 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
692 E = D->protocol_end();
693 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000694 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000695 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000696
697 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000698}
699
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000700bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
701 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
702 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
703 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000704
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000705 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000706}
707
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000708bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
709 ASTContext &Context = TU->getASTContext();
710
711 // Some builtin types (such as Objective-C's "id", "sel", and
712 // "Class") have associated declarations. Create cursors for those.
713 QualType VisitType;
714 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000715 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000716 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000717 case BuiltinType::Char_U:
718 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000719 case BuiltinType::Char16:
720 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000721 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000722 case BuiltinType::UInt:
723 case BuiltinType::ULong:
724 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000725 case BuiltinType::UInt128:
726 case BuiltinType::Char_S:
727 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000728 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000729 case BuiltinType::Short:
730 case BuiltinType::Int:
731 case BuiltinType::Long:
732 case BuiltinType::LongLong:
733 case BuiltinType::Int128:
734 case BuiltinType::Float:
735 case BuiltinType::Double:
736 case BuiltinType::LongDouble:
737 case BuiltinType::NullPtr:
738 case BuiltinType::Overload:
739 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000740 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000741
742 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000743 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000744
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000745 case BuiltinType::ObjCId:
746 VisitType = Context.getObjCIdType();
747 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000748
749 case BuiltinType::ObjCClass:
750 VisitType = Context.getObjCClassType();
751 break;
752
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000753 case BuiltinType::ObjCSel:
754 VisitType = Context.getObjCSelType();
755 break;
756 }
757
758 if (!VisitType.isNull()) {
759 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000760 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000761 TU));
762 }
763
764 return false;
765}
766
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000767bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
768 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
769}
770
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000771bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
772 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
773}
774
775bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
776 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
777}
778
779bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
780 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
781 return true;
782
783 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
784 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
785 TU)))
786 return true;
787 }
788
789 return false;
790}
791
792bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
793 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
794 return true;
795
796 if (TL.hasProtocolsAsWritten()) {
797 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000798 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000799 TL.getProtocolLoc(I),
800 TU)))
801 return true;
802 }
803 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000804
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000805 return false;
806}
807
808bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
809 return Visit(TL.getPointeeLoc());
810}
811
812bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
813 return Visit(TL.getPointeeLoc());
814}
815
816bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
817 return Visit(TL.getPointeeLoc());
818}
819
820bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000821 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000822}
823
824bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000825 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000826}
827
828bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
829 if (Visit(TL.getResultLoc()))
830 return true;
831
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000832 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
Ted Kremenek5dbacb42010-04-07 00:27:13 +0000833 if (Decl *D = TL.getArg(I))
834 if (Visit(MakeCXCursor(D, TU)))
835 return true;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000836
837 return false;
838}
839
840bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
841 if (Visit(TL.getElementLoc()))
842 return true;
843
844 if (Expr *Size = TL.getSizeExpr())
845 return Visit(MakeCXCursor(Size, StmtParent, TU));
846
847 return false;
848}
849
Douglas Gregor2332c112010-01-21 20:48:56 +0000850bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
851 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
852}
853
854bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
855 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
856 return Visit(TSInfo->getTypeLoc());
857
858 return false;
859}
860
Douglas Gregora59e3902010-01-21 23:27:09 +0000861bool CursorVisitor::VisitStmt(Stmt *S) {
862 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
863 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000864 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000865 return true;
866 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000867
Douglas Gregora59e3902010-01-21 23:27:09 +0000868 return false;
869}
870
871bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
872 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
873 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000874 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000875 return true;
876 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000877
Douglas Gregora59e3902010-01-21 23:27:09 +0000878 return false;
879}
880
Douglas Gregorf5bab412010-01-22 01:00:11 +0000881bool CursorVisitor::VisitIfStmt(IfStmt *S) {
882 if (VarDecl *Var = S->getConditionVariable()) {
883 if (Visit(MakeCXCursor(Var, TU)))
884 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000885 }
886
Douglas Gregor263b47b2010-01-25 16:12:32 +0000887 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
888 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000889 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
890 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000891 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
892 return true;
893
894 return false;
895}
896
897bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
898 if (VarDecl *Var = S->getConditionVariable()) {
899 if (Visit(MakeCXCursor(Var, TU)))
900 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000901 }
902
Douglas Gregor263b47b2010-01-25 16:12:32 +0000903 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
904 return true;
905 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
906 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000907
Douglas Gregor263b47b2010-01-25 16:12:32 +0000908 return false;
909}
910
911bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
912 if (VarDecl *Var = S->getConditionVariable()) {
913 if (Visit(MakeCXCursor(Var, TU)))
914 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000915 }
916
Douglas Gregor263b47b2010-01-25 16:12:32 +0000917 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
918 return true;
919 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000920 return true;
921
Douglas Gregor263b47b2010-01-25 16:12:32 +0000922 return false;
923}
924
925bool CursorVisitor::VisitForStmt(ForStmt *S) {
926 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
927 return true;
928 if (VarDecl *Var = S->getConditionVariable()) {
929 if (Visit(MakeCXCursor(Var, TU)))
930 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000931 }
932
Douglas Gregor263b47b2010-01-25 16:12:32 +0000933 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
934 return true;
935 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
936 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000937 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
938 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000939
Douglas Gregorf5bab412010-01-22 01:00:11 +0000940 return false;
941}
942
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000943bool CursorVisitor::VisitBlockExpr(BlockExpr *B) {
944 return Visit(B->getBlockDecl());
945}
946
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000947bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
948 // FIXME: Visit fields as well?
949 if (Visit(E->getTypeSourceInfo()->getTypeLoc()))
950 return true;
951
952 return VisitExpr(E);
953}
954
Douglas Gregor336fd812010-01-23 00:40:08 +0000955bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
956 if (E->isArgumentType()) {
957 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
958 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000959
Douglas Gregor336fd812010-01-23 00:40:08 +0000960 return false;
961 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000962
Douglas Gregor336fd812010-01-23 00:40:08 +0000963 return VisitExpr(E);
964}
965
966bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
967 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
968 if (Visit(TSInfo->getTypeLoc()))
969 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000970
Douglas Gregor336fd812010-01-23 00:40:08 +0000971 return VisitCastExpr(E);
972}
973
974bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
975 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
976 if (Visit(TSInfo->getTypeLoc()))
977 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000978
Douglas Gregor336fd812010-01-23 00:40:08 +0000979 return VisitExpr(E);
980}
981
Douglas Gregorc2350e52010-03-08 16:40:19 +0000982bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Douglas Gregor04badcf2010-04-21 00:45:42 +0000983 if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo())
984 if (Visit(TSInfo->getTypeLoc()))
985 return true;
Douglas Gregorc2350e52010-03-08 16:40:19 +0000986
987 return VisitExpr(E);
988}
989
Douglas Gregor81d34662010-04-20 15:39:42 +0000990bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
991 return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc());
992}
993
994
Ted Kremenek09dfa372010-02-18 05:46:33 +0000995bool CursorVisitor::VisitAttributes(Decl *D) {
996 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
997 if (Visit(MakeCXCursor(A, D, TU)))
998 return true;
999
1000 return false;
1001}
1002
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001003extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001004CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
1005 int displayDiagnostics) {
Douglas Gregora030b7c2010-01-22 20:35:53 +00001006 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001007 if (excludeDeclarationsFromPCH)
1008 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001009 if (displayDiagnostics)
1010 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001011 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +00001012}
1013
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001014void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001015 if (CIdx)
1016 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001017}
1018
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001019void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001020 if (CIdx) {
1021 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
1022 CXXIdx->setUseExternalASTGeneration(value);
1023 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001024}
1025
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001026CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +00001027 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001028 if (!CIdx)
1029 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001030
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00001031 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001032
Douglas Gregor28019772010-04-05 23:52:57 +00001033 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
1034 return ASTUnit::LoadFromPCHFile(ast_filename, Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +00001035 CXXIdx->getOnlyLocalDecls(),
1036 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +00001037}
1038
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001039CXTranslationUnit
1040clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
1041 const char *source_filename,
1042 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001043 const char **command_line_args,
1044 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001045 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001046 if (!CIdx)
1047 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001048
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001049 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
1050
Douglas Gregor5352ac02010-01-28 00:27:43 +00001051 // Configure the diagnostics.
1052 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +00001053 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
1054 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001055
Douglas Gregor4db64a42010-01-23 00:14:00 +00001056 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
1057 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattnera0a270c2010-04-05 22:42:27 +00001058 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001059 const llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001060 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001061 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
1062 Buffer));
1063 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001064
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001065 if (!CXXIdx->getUseExternalASTGeneration()) {
1066 llvm::SmallVector<const char *, 16> Args;
1067
1068 // The 'source_filename' argument is optional. If the caller does not
1069 // specify it then it is assumed that the source file is specified
1070 // in the actual argument list.
1071 if (source_filename)
1072 Args.push_back(source_filename);
1073 Args.insert(Args.end(), command_line_args,
1074 command_line_args + num_command_line_args);
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001075 Args.push_back("-Xclang");
1076 Args.push_back("-detailed-preprocessing-record");
Douglas Gregor5352ac02010-01-28 00:27:43 +00001077 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001078
Ted Kremenek29b72842010-01-07 22:49:05 +00001079#ifdef USE_CRASHTRACER
1080 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +00001081#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001082
Daniel Dunbar94220972009-12-05 02:17:18 +00001083 llvm::OwningPtr<ASTUnit> Unit(
1084 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +00001085 Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +00001086 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +00001087 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001088 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001089 RemappedFiles.size(),
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001090 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001091
Daniel Dunbar94220972009-12-05 02:17:18 +00001092 // FIXME: Until we have broader testing, just drop the entire AST if we
1093 // encountered an error.
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001094 if (NumErrors != Diags->getNumErrors()) {
Ted Kremenek34f6a322010-03-05 22:43:29 +00001095 // Make sure to check that 'Unit' is non-NULL.
1096 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
Douglas Gregor405634b2010-04-05 18:10:21 +00001097 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
1098 DEnd = Unit->stored_diag_end();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001099 D != DEnd; ++D) {
1100 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001101 CXString Msg = clang_formatDiagnostic(&Diag,
1102 clang_defaultDiagnosticDisplayOptions());
1103 fprintf(stderr, "%s\n", clang_getCString(Msg));
1104 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001105 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001106#ifdef LLVM_ON_WIN32
1107 // On Windows, force a flush, since there may be multiple copies of
1108 // stderr and stdout in the file system, all with different buffers
1109 // but writing to the same device.
1110 fflush(stderr);
Ted Kremenek83c51842010-03-26 01:34:51 +00001111#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001112 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001113 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001114
1115 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001116 }
1117
Ted Kremenek139ba862009-10-22 00:03:57 +00001118 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001119 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001120
Ted Kremenek139ba862009-10-22 00:03:57 +00001121 // First add the complete path to the 'clang' executable.
1122 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001123 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001124
Ted Kremenek139ba862009-10-22 00:03:57 +00001125 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001126 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001127
Ted Kremenek139ba862009-10-22 00:03:57 +00001128 // The 'source_filename' argument is optional. If the caller does not
1129 // specify it then it is assumed that the source file is specified
1130 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001131 if (source_filename)
1132 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001133
Steve Naroff37b5ac22009-10-15 20:50:09 +00001134 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001135 argv.push_back("-o");
Benjamin Kramerc2a98162010-03-13 21:22:49 +00001136 char astTmpFile[L_tmpnam];
1137 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001138
Douglas Gregor4db64a42010-01-23 00:14:00 +00001139 // Remap any unsaved files to temporary files.
1140 std::vector<llvm::sys::Path> TemporaryFiles;
1141 std::vector<std::string> RemapArgs;
1142 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1143 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001144
Douglas Gregor4db64a42010-01-23 00:14:00 +00001145 // The pointers into the elements of RemapArgs are stable because we
1146 // won't be adding anything to RemapArgs after this point.
1147 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1148 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001149
Ted Kremenek139ba862009-10-22 00:03:57 +00001150 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1151 for (int i = 0; i < num_command_line_args; ++i)
1152 if (const char *arg = command_line_args[i]) {
1153 if (strcmp(arg, "-o") == 0) {
1154 ++i; // Also skip the matching argument.
1155 continue;
1156 }
1157 if (strcmp(arg, "-emit-ast") == 0 ||
1158 strcmp(arg, "-c") == 0 ||
1159 strcmp(arg, "-fsyntax-only") == 0) {
1160 continue;
1161 }
1162
1163 // Keep the argument.
1164 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001165 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001166
Douglas Gregord93256e2010-01-28 06:00:51 +00001167 // Generate a temporary name for the diagnostics file.
Benjamin Kramerc2a98162010-03-13 21:22:49 +00001168 char tmpFileResults[L_tmpnam];
1169 char *tmpResultsFileName = tmpnam(tmpFileResults);
1170 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
Douglas Gregord93256e2010-01-28 06:00:51 +00001171 TemporaryFiles.push_back(DiagnosticsFile);
1172 argv.push_back("-fdiagnostics-binary");
1173
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001174 argv.push_back("-Xclang");
1175 argv.push_back("-detailed-preprocessing-record");
1176
Ted Kremenek139ba862009-10-22 00:03:57 +00001177 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001178 argv.push_back(NULL);
1179
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001180 // Invoke 'clang'.
1181 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1182 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001183 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001184 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1185 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001186 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001187 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001188 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001189
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001190 if (!ErrMsg.empty()) {
1191 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001192 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001193 I != E; ++I) {
1194 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001195 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001196 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001197 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001198
Daniel Dunbar32141c82010-02-23 20:23:45 +00001199 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001200 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001201
Douglas Gregor3687e9d2010-04-05 21:10:19 +00001202 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001203 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001204 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001205 RemappedFiles.size(),
1206 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001207 if (ATU) {
1208 LoadSerializedDiagnostics(DiagnosticsFile,
1209 num_unsaved_files, unsaved_files,
1210 ATU->getFileManager(),
1211 ATU->getSourceManager(),
Douglas Gregor405634b2010-04-05 18:10:21 +00001212 ATU->getStoredDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001213 } else if (CXXIdx->getDisplayDiagnostics()) {
1214 // We failed to load the ASTUnit, but we can still deserialize the
1215 // diagnostics and emit them.
1216 FileManager FileMgr;
Douglas Gregorf715ca12010-03-16 00:06:06 +00001217 Diagnostic Diag;
1218 SourceManager SourceMgr(Diag);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001219 // FIXME: Faked LangOpts!
1220 LangOptions LangOpts;
1221 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1222 LoadSerializedDiagnostics(DiagnosticsFile,
1223 num_unsaved_files, unsaved_files,
1224 FileMgr, SourceMgr, Diags);
1225 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1226 DEnd = Diags.end();
1227 D != DEnd; ++D) {
1228 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001229 CXString Msg = clang_formatDiagnostic(&Diag,
1230 clang_defaultDiagnosticDisplayOptions());
1231 fprintf(stderr, "%s\n", clang_getCString(Msg));
1232 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001233 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001234
1235#ifdef LLVM_ON_WIN32
1236 // On Windows, force a flush, since there may be multiple copies of
1237 // stderr and stdout in the file system, all with different buffers
1238 // but writing to the same device.
1239 fflush(stderr);
1240#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001241 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001242
Douglas Gregor313e26c2010-02-18 23:35:40 +00001243 if (ATU) {
1244 // Make the translation unit responsible for destroying all temporary files.
1245 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1246 ATU->addTemporaryFile(TemporaryFiles[i]);
1247 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1248 } else {
1249 // Destroy all of the temporary files now; they can't be referenced any
1250 // longer.
1251 llvm::sys::Path(astTmpFile).eraseFromDisk();
1252 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1253 TemporaryFiles[i].eraseFromDisk();
1254 }
1255
Steve Naroffe19944c2009-10-15 22:23:48 +00001256 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001257}
1258
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001259void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001260 if (CTUnit)
1261 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001262}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001263
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001264CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001265 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001266 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001267
Steve Naroff77accc12009-09-03 18:19:54 +00001268 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001269 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001270}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001271
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001272CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001273 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001274 return Result;
1275}
1276
Ted Kremenekfb480492010-01-13 21:46:36 +00001277} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001278
Ted Kremenekfb480492010-01-13 21:46:36 +00001279//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001280// CXSourceLocation and CXSourceRange Operations.
1281//===----------------------------------------------------------------------===//
1282
Douglas Gregorb9790342010-01-22 21:44:22 +00001283extern "C" {
1284CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001285 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001286 return Result;
1287}
1288
1289unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001290 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1291 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1292 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001293}
1294
1295CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1296 CXFile file,
1297 unsigned line,
1298 unsigned column) {
1299 if (!tu)
1300 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001301
Douglas Gregorb9790342010-01-22 21:44:22 +00001302 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1303 SourceLocation SLoc
1304 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001305 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001306 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001307
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001308 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001309}
1310
Douglas Gregor5352ac02010-01-28 00:27:43 +00001311CXSourceRange clang_getNullRange() {
1312 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1313 return Result;
1314}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001315
Douglas Gregor5352ac02010-01-28 00:27:43 +00001316CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1317 if (begin.ptr_data[0] != end.ptr_data[0] ||
1318 begin.ptr_data[1] != end.ptr_data[1])
1319 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001320
1321 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001322 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001323 return Result;
1324}
1325
Douglas Gregor46766dc2010-01-26 19:19:08 +00001326void clang_getInstantiationLocation(CXSourceLocation location,
1327 CXFile *file,
1328 unsigned *line,
1329 unsigned *column,
1330 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001331 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1332
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001333 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001334 if (file)
1335 *file = 0;
1336 if (line)
1337 *line = 0;
1338 if (column)
1339 *column = 0;
1340 if (offset)
1341 *offset = 0;
1342 return;
1343 }
1344
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001345 const SourceManager &SM =
1346 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001347 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001348
1349 if (file)
1350 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1351 if (line)
1352 *line = SM.getInstantiationLineNumber(InstLoc);
1353 if (column)
1354 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001355 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001356 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001357}
1358
Douglas Gregor1db19de2010-01-19 21:36:55 +00001359CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001360 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001361 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001362 return Result;
1363}
1364
1365CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001366 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001367 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001368 return Result;
1369}
1370
Douglas Gregorb9790342010-01-22 21:44:22 +00001371} // end: extern "C"
1372
Douglas Gregor1db19de2010-01-19 21:36:55 +00001373//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001374// CXFile Operations.
1375//===----------------------------------------------------------------------===//
1376
1377extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001378CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001379 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001380 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001381
Steve Naroff88145032009-10-27 14:35:18 +00001382 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001383 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001384}
1385
1386time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001387 if (!SFile)
1388 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001389
Steve Naroff88145032009-10-27 14:35:18 +00001390 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1391 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001392}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001393
Douglas Gregorb9790342010-01-22 21:44:22 +00001394CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1395 if (!tu)
1396 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001397
Douglas Gregorb9790342010-01-22 21:44:22 +00001398 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001399
Douglas Gregorb9790342010-01-22 21:44:22 +00001400 FileManager &FMgr = CXXUnit->getFileManager();
1401 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1402 return const_cast<FileEntry *>(File);
1403}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001404
Ted Kremenekfb480492010-01-13 21:46:36 +00001405} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001406
Ted Kremenekfb480492010-01-13 21:46:36 +00001407//===----------------------------------------------------------------------===//
1408// CXCursor Operations.
1409//===----------------------------------------------------------------------===//
1410
Ted Kremenekfb480492010-01-13 21:46:36 +00001411static Decl *getDeclFromExpr(Stmt *E) {
1412 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1413 return RefExpr->getDecl();
1414 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1415 return ME->getMemberDecl();
1416 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1417 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001418
Ted Kremenekfb480492010-01-13 21:46:36 +00001419 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1420 return getDeclFromExpr(CE->getCallee());
1421 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1422 return getDeclFromExpr(CE->getSubExpr());
1423 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1424 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001425
Ted Kremenekfb480492010-01-13 21:46:36 +00001426 return 0;
1427}
1428
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001429static SourceLocation getLocationFromExpr(Expr *E) {
1430 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1431 return /*FIXME:*/Msg->getLeftLoc();
1432 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1433 return DRE->getLocation();
1434 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1435 return Member->getMemberLoc();
1436 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1437 return Ivar->getLocation();
1438 return E->getLocStart();
1439}
1440
Ted Kremenekfb480492010-01-13 21:46:36 +00001441extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001442
1443unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001444 CXCursorVisitor visitor,
1445 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001446 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001447
1448 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001449
Douglas Gregorb1373d02010-01-20 20:59:29 +00001450 // Set the PCHLevel to filter out unwanted decls if requested.
1451 if (CXXUnit->getOnlyLocalDecls()) {
1452 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001453
Douglas Gregorb1373d02010-01-20 20:59:29 +00001454 // If the main input was an AST, bump the level.
1455 if (CXXUnit->isMainFileAST())
1456 ++PCHLevel;
1457 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001458
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001459 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001460 return CursorVis.VisitChildren(parent);
1461}
1462
Douglas Gregor78205d42010-01-20 21:45:58 +00001463static CXString getDeclSpelling(Decl *D) {
1464 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1465 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001466 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001467
Douglas Gregor78205d42010-01-20 21:45:58 +00001468 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001469 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001470
Douglas Gregor78205d42010-01-20 21:45:58 +00001471 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1472 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1473 // and returns different names. NamedDecl returns the class name and
1474 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001475 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001476
Douglas Gregor78205d42010-01-20 21:45:58 +00001477 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001478 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001479
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001480 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001481}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001482
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001483CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001484 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001485 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001486
Steve Narofff334b4e2009-09-02 18:26:48 +00001487 if (clang_isReference(C.kind)) {
1488 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001489 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001490 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001491 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001492 }
1493 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001494 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001495 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001496 }
1497 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001498 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001499 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001500 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001501 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001502 case CXCursor_TypeRef: {
1503 TypeDecl *Type = getCursorTypeRef(C).first;
1504 assert(Type && "Missing type decl");
1505
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001506 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1507 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001508 }
1509
Daniel Dunbaracca7252009-11-30 20:42:49 +00001510 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001511 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001512 }
1513 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001514
1515 if (clang_isExpression(C.kind)) {
1516 Decl *D = getDeclFromExpr(getCursorExpr(C));
1517 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001518 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001519 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001520 }
1521
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001522 if (C.kind == CXCursor_MacroInstantiation)
1523 return createCXString(getCursorMacroInstantiation(C)->getName()
1524 ->getNameStart());
1525
Douglas Gregor572feb22010-03-18 18:04:21 +00001526 if (C.kind == CXCursor_MacroDefinition)
1527 return createCXString(getCursorMacroDefinition(C)->getName()
1528 ->getNameStart());
1529
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001530 if (clang_isDeclaration(C.kind))
1531 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001532
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001533 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001534}
1535
Ted Kremeneke68fff62010-02-17 00:41:32 +00001536CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001537 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001538 case CXCursor_FunctionDecl:
1539 return createCXString("FunctionDecl");
1540 case CXCursor_TypedefDecl:
1541 return createCXString("TypedefDecl");
1542 case CXCursor_EnumDecl:
1543 return createCXString("EnumDecl");
1544 case CXCursor_EnumConstantDecl:
1545 return createCXString("EnumConstantDecl");
1546 case CXCursor_StructDecl:
1547 return createCXString("StructDecl");
1548 case CXCursor_UnionDecl:
1549 return createCXString("UnionDecl");
1550 case CXCursor_ClassDecl:
1551 return createCXString("ClassDecl");
1552 case CXCursor_FieldDecl:
1553 return createCXString("FieldDecl");
1554 case CXCursor_VarDecl:
1555 return createCXString("VarDecl");
1556 case CXCursor_ParmDecl:
1557 return createCXString("ParmDecl");
1558 case CXCursor_ObjCInterfaceDecl:
1559 return createCXString("ObjCInterfaceDecl");
1560 case CXCursor_ObjCCategoryDecl:
1561 return createCXString("ObjCCategoryDecl");
1562 case CXCursor_ObjCProtocolDecl:
1563 return createCXString("ObjCProtocolDecl");
1564 case CXCursor_ObjCPropertyDecl:
1565 return createCXString("ObjCPropertyDecl");
1566 case CXCursor_ObjCIvarDecl:
1567 return createCXString("ObjCIvarDecl");
1568 case CXCursor_ObjCInstanceMethodDecl:
1569 return createCXString("ObjCInstanceMethodDecl");
1570 case CXCursor_ObjCClassMethodDecl:
1571 return createCXString("ObjCClassMethodDecl");
1572 case CXCursor_ObjCImplementationDecl:
1573 return createCXString("ObjCImplementationDecl");
1574 case CXCursor_ObjCCategoryImplDecl:
1575 return createCXString("ObjCCategoryImplDecl");
Ted Kremenek8bd5a692010-04-13 23:39:06 +00001576 case CXCursor_CXXMethod:
1577 return createCXString("CXXMethod");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001578 case CXCursor_UnexposedDecl:
1579 return createCXString("UnexposedDecl");
1580 case CXCursor_ObjCSuperClassRef:
1581 return createCXString("ObjCSuperClassRef");
1582 case CXCursor_ObjCProtocolRef:
1583 return createCXString("ObjCProtocolRef");
1584 case CXCursor_ObjCClassRef:
1585 return createCXString("ObjCClassRef");
1586 case CXCursor_TypeRef:
1587 return createCXString("TypeRef");
1588 case CXCursor_UnexposedExpr:
1589 return createCXString("UnexposedExpr");
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00001590 case CXCursor_BlockExpr:
1591 return createCXString("BlockExpr");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001592 case CXCursor_DeclRefExpr:
1593 return createCXString("DeclRefExpr");
1594 case CXCursor_MemberRefExpr:
1595 return createCXString("MemberRefExpr");
1596 case CXCursor_CallExpr:
1597 return createCXString("CallExpr");
1598 case CXCursor_ObjCMessageExpr:
1599 return createCXString("ObjCMessageExpr");
1600 case CXCursor_UnexposedStmt:
1601 return createCXString("UnexposedStmt");
1602 case CXCursor_InvalidFile:
1603 return createCXString("InvalidFile");
Ted Kremenek292db642010-03-19 20:39:05 +00001604 case CXCursor_InvalidCode:
1605 return createCXString("InvalidCode");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001606 case CXCursor_NoDeclFound:
1607 return createCXString("NoDeclFound");
1608 case CXCursor_NotImplemented:
1609 return createCXString("NotImplemented");
1610 case CXCursor_TranslationUnit:
1611 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001612 case CXCursor_UnexposedAttr:
1613 return createCXString("UnexposedAttr");
1614 case CXCursor_IBActionAttr:
1615 return createCXString("attribute(ibaction)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001616 case CXCursor_IBOutletAttr:
1617 return createCXString("attribute(iboutlet)");
1618 case CXCursor_PreprocessingDirective:
1619 return createCXString("preprocessing directive");
Douglas Gregor572feb22010-03-18 18:04:21 +00001620 case CXCursor_MacroDefinition:
1621 return createCXString("macro definition");
Douglas Gregor48072312010-03-18 15:23:44 +00001622 case CXCursor_MacroInstantiation:
1623 return createCXString("macro instantiation");
Steve Naroff89922f82009-08-31 00:59:03 +00001624 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001625
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001626 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001627 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001628}
Steve Naroff89922f82009-08-31 00:59:03 +00001629
Ted Kremeneke68fff62010-02-17 00:41:32 +00001630enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1631 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001632 CXClientData client_data) {
1633 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1634 *BestCursor = cursor;
1635 return CXChildVisit_Recurse;
1636}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001637
Douglas Gregorb9790342010-01-22 21:44:22 +00001638CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1639 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001640 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001641
Douglas Gregorb9790342010-01-22 21:44:22 +00001642 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1643
Douglas Gregorbdf60622010-03-05 21:16:25 +00001644 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1645
Ted Kremeneka297de22010-01-25 22:34:44 +00001646 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001647 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1648 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001649 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001650
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001651 // FIXME: Would be great to have a "hint" cursor, then walk from that
1652 // hint cursor upward until we find a cursor whose source range encloses
1653 // the region of interest, rather than starting from the translation unit.
1654 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001655 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001656 Decl::MaxPCHLevel, RegionOfInterest);
1657 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001658 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001659 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001660}
1661
Ted Kremenek73885552009-11-17 19:28:59 +00001662CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001663 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001664}
1665
1666unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001667 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001668}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001669
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001670unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001671 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1672}
1673
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001674unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001675 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1676}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001677
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001678unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001679 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1680}
1681
Douglas Gregor97b98722010-01-19 23:20:36 +00001682unsigned clang_isExpression(enum CXCursorKind K) {
1683 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1684}
1685
1686unsigned clang_isStatement(enum CXCursorKind K) {
1687 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1688}
1689
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001690unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1691 return K == CXCursor_TranslationUnit;
1692}
1693
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001694unsigned clang_isPreprocessing(enum CXCursorKind K) {
1695 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
1696}
1697
Ted Kremenekad6eff62010-03-08 21:17:29 +00001698unsigned clang_isUnexposed(enum CXCursorKind K) {
1699 switch (K) {
1700 case CXCursor_UnexposedDecl:
1701 case CXCursor_UnexposedExpr:
1702 case CXCursor_UnexposedStmt:
1703 case CXCursor_UnexposedAttr:
1704 return true;
1705 default:
1706 return false;
1707 }
1708}
1709
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001710CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001711 return C.kind;
1712}
1713
Douglas Gregor98258af2010-01-18 22:46:11 +00001714CXSourceLocation clang_getCursorLocation(CXCursor C) {
1715 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001716 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001717 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001718 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1719 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001720 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001721 }
1722
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001723 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001724 std::pair<ObjCProtocolDecl *, SourceLocation> P
1725 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001726 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001727 }
1728
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001729 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001730 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1731 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001732 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001733 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001734
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001735 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001736 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001737 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001738 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001739
Douglas Gregorf46034a2010-01-18 23:41:10 +00001740 default:
1741 // FIXME: Need a way to enumerate all non-reference cases.
1742 llvm_unreachable("Missed a reference kind");
1743 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001744 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001745
1746 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001747 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001748 getLocationFromExpr(getCursorExpr(C)));
1749
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001750 if (C.kind == CXCursor_PreprocessingDirective) {
1751 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
1752 return cxloc::translateSourceLocation(getCursorContext(C), L);
1753 }
Douglas Gregor48072312010-03-18 15:23:44 +00001754
1755 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001756 SourceLocation L
1757 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
Douglas Gregor48072312010-03-18 15:23:44 +00001758 return cxloc::translateSourceLocation(getCursorContext(C), L);
1759 }
Douglas Gregor572feb22010-03-18 18:04:21 +00001760
1761 if (C.kind == CXCursor_MacroDefinition) {
1762 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
1763 return cxloc::translateSourceLocation(getCursorContext(C), L);
1764 }
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001765
Douglas Gregor5352ac02010-01-28 00:27:43 +00001766 if (!getCursorDecl(C))
1767 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001768
Douglas Gregorf46034a2010-01-18 23:41:10 +00001769 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001770 SourceLocation Loc = D->getLocation();
1771 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1772 Loc = Class->getClassLoc();
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00001773 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001774}
Douglas Gregora7bde202010-01-19 00:34:46 +00001775
1776CXSourceRange clang_getCursorExtent(CXCursor C) {
1777 if (clang_isReference(C.kind)) {
1778 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001779 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001780 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1781 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001782 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001783 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001784
1785 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001786 std::pair<ObjCProtocolDecl *, SourceLocation> P
1787 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001788 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001789 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001790
1791 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001792 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1793 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001794
Ted Kremeneka297de22010-01-25 22:34:44 +00001795 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001796 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001797
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001798 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001799 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001800 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001801 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001802
Douglas Gregora7bde202010-01-19 00:34:46 +00001803 default:
1804 // FIXME: Need a way to enumerate all non-reference cases.
1805 llvm_unreachable("Missed a reference kind");
1806 }
1807 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001808
1809 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001810 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001811 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001812
1813 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001814 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001815 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001816
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001817 if (C.kind == CXCursor_PreprocessingDirective) {
1818 SourceRange R = cxcursor::getCursorPreprocessingDirective(C);
1819 return cxloc::translateSourceRange(getCursorContext(C), R);
1820 }
Douglas Gregor48072312010-03-18 15:23:44 +00001821
1822 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001823 SourceRange R = cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +00001824 return cxloc::translateSourceRange(getCursorContext(C), R);
1825 }
Douglas Gregor572feb22010-03-18 18:04:21 +00001826
1827 if (C.kind == CXCursor_MacroDefinition) {
1828 SourceRange R = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
1829 return cxloc::translateSourceRange(getCursorContext(C), R);
1830 }
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001831
Douglas Gregor5352ac02010-01-28 00:27:43 +00001832 if (!getCursorDecl(C))
1833 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001834
Douglas Gregora7bde202010-01-19 00:34:46 +00001835 Decl *D = getCursorDecl(C);
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00001836 return cxloc::translateSourceRange(getCursorContext(C), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001837}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001838
1839CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001840 if (clang_isInvalid(C.kind))
1841 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001842
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001843 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001844 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001845 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001846
Douglas Gregor97b98722010-01-19 23:20:36 +00001847 if (clang_isExpression(C.kind)) {
1848 Decl *D = getDeclFromExpr(getCursorExpr(C));
1849 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001850 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001851 return clang_getNullCursor();
1852 }
1853
Douglas Gregorbf7efa22010-03-18 18:23:03 +00001854 if (C.kind == CXCursor_MacroInstantiation) {
1855 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
1856 return MakeMacroDefinitionCursor(Def, CXXUnit);
1857 }
1858
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001859 if (!clang_isReference(C.kind))
1860 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001861
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001862 switch (C.kind) {
1863 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001864 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001865
1866 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001867 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001868
1869 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001870 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001871
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001872 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001873 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001874
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001875 default:
1876 // We would prefer to enumerate all non-reference cursor kinds here.
1877 llvm_unreachable("Unhandled reference cursor kind");
1878 break;
1879 }
1880 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001881
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001882 return clang_getNullCursor();
1883}
1884
Douglas Gregorb6998662010-01-19 19:34:47 +00001885CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001886 if (clang_isInvalid(C.kind))
1887 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001888
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001889 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001890
Douglas Gregorb6998662010-01-19 19:34:47 +00001891 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001892 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001893 C = clang_getCursorReferenced(C);
1894 WasReference = true;
1895 }
1896
Douglas Gregorbf7efa22010-03-18 18:23:03 +00001897 if (C.kind == CXCursor_MacroInstantiation)
1898 return clang_getCursorReferenced(C);
1899
Douglas Gregorb6998662010-01-19 19:34:47 +00001900 if (!clang_isDeclaration(C.kind))
1901 return clang_getNullCursor();
1902
1903 Decl *D = getCursorDecl(C);
1904 if (!D)
1905 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001906
Douglas Gregorb6998662010-01-19 19:34:47 +00001907 switch (D->getKind()) {
1908 // Declaration kinds that don't really separate the notions of
1909 // declaration and definition.
1910 case Decl::Namespace:
1911 case Decl::Typedef:
1912 case Decl::TemplateTypeParm:
1913 case Decl::EnumConstant:
1914 case Decl::Field:
1915 case Decl::ObjCIvar:
1916 case Decl::ObjCAtDefsField:
1917 case Decl::ImplicitParam:
1918 case Decl::ParmVar:
1919 case Decl::NonTypeTemplateParm:
1920 case Decl::TemplateTemplateParm:
1921 case Decl::ObjCCategoryImpl:
1922 case Decl::ObjCImplementation:
1923 case Decl::LinkageSpec:
1924 case Decl::ObjCPropertyImpl:
1925 case Decl::FileScopeAsm:
1926 case Decl::StaticAssert:
1927 case Decl::Block:
1928 return C;
1929
1930 // Declaration kinds that don't make any sense here, but are
1931 // nonetheless harmless.
1932 case Decl::TranslationUnit:
Douglas Gregorb6998662010-01-19 19:34:47 +00001933 break;
1934
1935 // Declaration kinds for which the definition is not resolvable.
1936 case Decl::UnresolvedUsingTypename:
1937 case Decl::UnresolvedUsingValue:
1938 break;
1939
1940 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001941 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1942 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001943
1944 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001945 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001946
1947 case Decl::Enum:
1948 case Decl::Record:
1949 case Decl::CXXRecord:
1950 case Decl::ClassTemplateSpecialization:
1951 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001952 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001953 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001954 return clang_getNullCursor();
1955
1956 case Decl::Function:
1957 case Decl::CXXMethod:
1958 case Decl::CXXConstructor:
1959 case Decl::CXXDestructor:
1960 case Decl::CXXConversion: {
1961 const FunctionDecl *Def = 0;
1962 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001963 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001964 return clang_getNullCursor();
1965 }
1966
1967 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001968 // Ask the variable if it has a definition.
1969 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1970 return MakeCXCursor(Def, CXXUnit);
1971 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001972 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001973
Douglas Gregorb6998662010-01-19 19:34:47 +00001974 case Decl::FunctionTemplate: {
1975 const FunctionDecl *Def = 0;
1976 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001977 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001978 return clang_getNullCursor();
1979 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001980
Douglas Gregorb6998662010-01-19 19:34:47 +00001981 case Decl::ClassTemplate: {
1982 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001983 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001984 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001985 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001986 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001987 return clang_getNullCursor();
1988 }
1989
1990 case Decl::Using: {
1991 UsingDecl *Using = cast<UsingDecl>(D);
1992 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001993 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1994 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001995 S != SEnd; ++S) {
1996 if (Def != clang_getNullCursor()) {
1997 // FIXME: We have no way to return multiple results.
1998 return clang_getNullCursor();
1999 }
2000
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002001 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002002 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002003 }
2004
2005 return Def;
2006 }
2007
2008 case Decl::UsingShadow:
2009 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002010 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002011 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002012
2013 case Decl::ObjCMethod: {
2014 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
2015 if (Method->isThisDeclarationADefinition())
2016 return C;
2017
2018 // Dig out the method definition in the associated
2019 // @implementation, if we have it.
2020 // FIXME: The ASTs should make finding the definition easier.
2021 if (ObjCInterfaceDecl *Class
2022 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
2023 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
2024 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
2025 Method->isInstanceMethod()))
2026 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002027 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002028
2029 return clang_getNullCursor();
2030 }
2031
2032 case Decl::ObjCCategory:
2033 if (ObjCCategoryImplDecl *Impl
2034 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002035 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002036 return clang_getNullCursor();
2037
2038 case Decl::ObjCProtocol:
2039 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
2040 return C;
2041 return clang_getNullCursor();
2042
2043 case Decl::ObjCInterface:
2044 // There are two notions of a "definition" for an Objective-C
2045 // class: the interface and its implementation. When we resolved a
2046 // reference to an Objective-C class, produce the @interface as
2047 // the definition; when we were provided with the interface,
2048 // produce the @implementation as the definition.
2049 if (WasReference) {
2050 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
2051 return C;
2052 } else if (ObjCImplementationDecl *Impl
2053 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002054 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002055 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002056
Douglas Gregorb6998662010-01-19 19:34:47 +00002057 case Decl::ObjCProperty:
2058 // FIXME: We don't really know where to find the
2059 // ObjCPropertyImplDecls that implement this property.
2060 return clang_getNullCursor();
2061
2062 case Decl::ObjCCompatibleAlias:
2063 if (ObjCInterfaceDecl *Class
2064 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
2065 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002066 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002067
Douglas Gregorb6998662010-01-19 19:34:47 +00002068 return clang_getNullCursor();
2069
2070 case Decl::ObjCForwardProtocol: {
2071 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
2072 if (Forward->protocol_size() == 1)
2073 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002074 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002075 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002076
2077 // FIXME: Cannot return multiple definitions.
2078 return clang_getNullCursor();
2079 }
2080
2081 case Decl::ObjCClass: {
2082 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
2083 if (Class->size() == 1) {
2084 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
2085 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002086 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002087 return clang_getNullCursor();
2088 }
2089
2090 // FIXME: Cannot return multiple definitions.
2091 return clang_getNullCursor();
2092 }
2093
2094 case Decl::Friend:
2095 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002096 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002097 return clang_getNullCursor();
2098
2099 case Decl::FriendTemplate:
2100 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002101 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002102 return clang_getNullCursor();
2103 }
2104
2105 return clang_getNullCursor();
2106}
2107
2108unsigned clang_isCursorDefinition(CXCursor C) {
2109 if (!clang_isDeclaration(C.kind))
2110 return 0;
2111
2112 return clang_getCursorDefinition(C) == C;
2113}
2114
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002115void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00002116 const char **startBuf,
2117 const char **endBuf,
2118 unsigned *startLine,
2119 unsigned *startColumn,
2120 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002121 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00002122 assert(getCursorDecl(C) && "CXCursor has null decl");
2123 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00002124 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
2125 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002126
Steve Naroff4ade6d62009-09-23 17:52:52 +00002127 SourceManager &SM = FD->getASTContext().getSourceManager();
2128 *startBuf = SM.getCharacterData(Body->getLBracLoc());
2129 *endBuf = SM.getCharacterData(Body->getRBracLoc());
2130 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
2131 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
2132 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
2133 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
2134}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002135
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002136void clang_enableStackTraces(void) {
2137 llvm::sys::PrintStackTraceOnErrorSignal();
2138}
2139
Ted Kremenekfb480492010-01-13 21:46:36 +00002140} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00002141
Ted Kremenekfb480492010-01-13 21:46:36 +00002142//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002143// Token-based Operations.
2144//===----------------------------------------------------------------------===//
2145
2146/* CXToken layout:
2147 * int_data[0]: a CXTokenKind
2148 * int_data[1]: starting token location
2149 * int_data[2]: token length
2150 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002151 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002152 * otherwise unused.
2153 */
2154extern "C" {
2155
2156CXTokenKind clang_getTokenKind(CXToken CXTok) {
2157 return static_cast<CXTokenKind>(CXTok.int_data[0]);
2158}
2159
2160CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
2161 switch (clang_getTokenKind(CXTok)) {
2162 case CXToken_Identifier:
2163 case CXToken_Keyword:
2164 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002165 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2166 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002167
2168 case CXToken_Literal: {
2169 // We have stashed the starting pointer in the ptr_data field. Use it.
2170 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002171 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002172 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002173
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002174 case CXToken_Punctuation:
2175 case CXToken_Comment:
2176 break;
2177 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002178
2179 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002180 // deconstructing the source location.
2181 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2182 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002183 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002184
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002185 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2186 std::pair<FileID, unsigned> LocInfo
2187 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +00002188 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002189 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002190 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
2191 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00002192 return createCXString("");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002193
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002194 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002195}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002196
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002197CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2198 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2199 if (!CXXUnit)
2200 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002201
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002202 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2203 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2204}
2205
2206CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2207 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002208 if (!CXXUnit)
2209 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002210
2211 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002212 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2213}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002214
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002215void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2216 CXToken **Tokens, unsigned *NumTokens) {
2217 if (Tokens)
2218 *Tokens = 0;
2219 if (NumTokens)
2220 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002221
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002222 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2223 if (!CXXUnit || !Tokens || !NumTokens)
2224 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002225
Douglas Gregorbdf60622010-03-05 21:16:25 +00002226 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2227
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002228 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002229 if (R.isInvalid())
2230 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002231
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002232 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2233 std::pair<FileID, unsigned> BeginLocInfo
2234 = SourceMgr.getDecomposedLoc(R.getBegin());
2235 std::pair<FileID, unsigned> EndLocInfo
2236 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002237
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002238 // Cannot tokenize across files.
2239 if (BeginLocInfo.first != EndLocInfo.first)
2240 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002241
2242 // Create a lexer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002243 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002244 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002245 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
Douglas Gregor47a3fcd2010-03-16 20:26:15 +00002246 if (Invalid)
2247 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +00002248
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002249 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2250 CXXUnit->getASTContext().getLangOptions(),
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002251 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002252 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002253
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002254 // Lex tokens until we hit the end of the range.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002255 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002256 llvm::SmallVector<CXToken, 32> CXTokens;
2257 Token Tok;
2258 do {
2259 // Lex the next token
2260 Lex.LexFromRawLexer(Tok);
2261 if (Tok.is(tok::eof))
2262 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002263
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002264 // Initialize the CXToken.
2265 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002266
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002267 // - Common fields
2268 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2269 CXTok.int_data[2] = Tok.getLength();
2270 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002271
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002272 // - Kind-specific fields
2273 if (Tok.isLiteral()) {
2274 CXTok.int_data[0] = CXToken_Literal;
2275 CXTok.ptr_data = (void *)Tok.getLiteralData();
2276 } else if (Tok.is(tok::identifier)) {
Douglas Gregoraea67db2010-03-15 22:54:52 +00002277 // Lookup the identifier to determine whether we have a keyword.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002278 std::pair<FileID, unsigned> LocInfo
2279 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Douglas Gregorf715ca12010-03-16 00:06:06 +00002280 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002281 llvm::StringRef Buf
Douglas Gregorf715ca12010-03-16 00:06:06 +00002282 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
2283 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00002284 return;
2285
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002286 const char *StartPos = Buf.data() + LocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002287 IdentifierInfo *II
2288 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2289 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2290 CXToken_Identifier
2291 : CXToken_Keyword;
2292 CXTok.ptr_data = II;
2293 } else if (Tok.is(tok::comment)) {
2294 CXTok.int_data[0] = CXToken_Comment;
2295 CXTok.ptr_data = 0;
2296 } else {
2297 CXTok.int_data[0] = CXToken_Punctuation;
2298 CXTok.ptr_data = 0;
2299 }
2300 CXTokens.push_back(CXTok);
2301 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002302
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002303 if (CXTokens.empty())
2304 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002305
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002306 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2307 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2308 *NumTokens = CXTokens.size();
2309}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002310
2311typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2312
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002313enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2314 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002315 CXClientData client_data) {
2316 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2317
2318 // We only annotate the locations of declarations, simple
2319 // references, and expressions which directly reference something.
2320 CXCursorKind Kind = clang_getCursorKind(cursor);
2321 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2322 // Okay: We can annotate the location of this declaration with the
2323 // declaration or reference
2324 } else if (clang_isExpression(cursor.kind)) {
2325 if (Kind != CXCursor_DeclRefExpr &&
2326 Kind != CXCursor_MemberRefExpr &&
2327 Kind != CXCursor_ObjCMessageExpr)
2328 return CXChildVisit_Recurse;
2329
2330 CXCursor Referenced = clang_getCursorReferenced(cursor);
2331 if (Referenced == cursor || Referenced == clang_getNullCursor())
2332 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002333
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002334 // Okay: we can annotate the location of this expression
Douglas Gregor0396f462010-03-19 05:22:59 +00002335 } else if (clang_isPreprocessing(cursor.kind)) {
2336 // We can always annotate a preprocessing directive/macro instantiation.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002337 } else {
2338 // Nothing to annotate
2339 return CXChildVisit_Recurse;
2340 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002341
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002342 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2343 (*Data)[Loc.int_data] = cursor;
2344 return CXChildVisit_Recurse;
2345}
2346
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002347void clang_annotateTokens(CXTranslationUnit TU,
2348 CXToken *Tokens, unsigned NumTokens,
2349 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002350 if (NumTokens == 0)
2351 return;
2352
2353 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002354 for (unsigned I = 0; I != NumTokens; ++I)
2355 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002356
2357 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2358 if (!CXXUnit || !Tokens)
2359 return;
2360
Douglas Gregorbdf60622010-03-05 21:16:25 +00002361 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2362
Douglas Gregor0396f462010-03-19 05:22:59 +00002363 // Determine the region of interest, which contains all of the tokens.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002364 SourceRange RegionOfInterest;
2365 RegionOfInterest.setBegin(
2366 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2367 SourceLocation End
Douglas Gregor09d9fa12010-04-05 16:10:30 +00002368 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0396f462010-03-19 05:22:59 +00002369 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002370 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor2507fa82010-03-19 00:18:31 +00002371
Douglas Gregor0396f462010-03-19 05:22:59 +00002372 // A mapping from the source locations found when re-lexing or traversing the
2373 // region of interest to the corresponding cursors.
2374 AnnotateTokensData Annotated;
2375
2376 // Relex the tokens within the source range to look for preprocessing
2377 // directives.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002378 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2379 std::pair<FileID, unsigned> BeginLocInfo
2380 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
2381 std::pair<FileID, unsigned> EndLocInfo
2382 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
2383
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002384 llvm::StringRef Buffer;
Douglas Gregor0396f462010-03-19 05:22:59 +00002385 bool Invalid = false;
2386 if (BeginLocInfo.first == EndLocInfo.first &&
2387 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
2388 !Invalid) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002389 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2390 CXXUnit->getASTContext().getLangOptions(),
Douglas Gregor4ae8f292010-03-18 17:52:52 +00002391 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
2392 Buffer.end());
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002393 Lex.SetCommentRetentionState(true);
2394
2395 // Lex tokens in raw mode until we hit the end of the range, to avoid
2396 // entering #includes or expanding macros.
Douglas Gregor48072312010-03-18 15:23:44 +00002397 while (true) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002398 Token Tok;
2399 Lex.LexFromRawLexer(Tok);
2400
2401 reprocess:
2402 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
2403 // We have found a preprocessing directive. Gobble it up so that we
2404 // don't see it while preprocessing these tokens later, but keep track of
2405 // all of the token locations inside this preprocessing directive so that
2406 // we can annotate them appropriately.
2407 //
2408 // FIXME: Some simple tests here could identify macro definitions and
2409 // #undefs, to provide specific cursor kinds for those.
2410 std::vector<SourceLocation> Locations;
2411 do {
2412 Locations.push_back(Tok.getLocation());
2413 Lex.LexFromRawLexer(Tok);
2414 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
2415
2416 using namespace cxcursor;
2417 CXCursor Cursor
2418 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
2419 Locations.back()),
2420 CXXUnit);
2421 for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
2422 Annotated[Locations[I].getRawEncoding()] = Cursor;
2423 }
2424
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002425 if (Tok.isAtStartOfLine())
2426 goto reprocess;
2427
2428 continue;
2429 }
2430
Douglas Gregor48072312010-03-18 15:23:44 +00002431 if (Tok.is(tok::eof))
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002432 break;
2433 }
Douglas Gregor4ae8f292010-03-18 17:52:52 +00002434 }
Douglas Gregor0396f462010-03-19 05:22:59 +00002435
2436 // Annotate all of the source locations in the region of interest that map to
2437 // a specific cursor.
2438 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
2439 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
2440 Decl::MaxPCHLevel, RegionOfInterest);
2441 AnnotateVis.VisitChildren(Parent);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002442
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002443 for (unsigned I = 0; I != NumTokens; ++I) {
2444 // Determine whether we saw a cursor at this token's location.
2445 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2446 if (Pos == Annotated.end())
2447 continue;
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002448
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002449 Cursors[I] = Pos->second;
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002450 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002451}
2452
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002453void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002454 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002455 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002456}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002457
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002458} // end: extern "C"
2459
2460//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002461// Operations for querying linkage of a cursor.
2462//===----------------------------------------------------------------------===//
2463
2464extern "C" {
2465CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
Douglas Gregor0396f462010-03-19 05:22:59 +00002466 if (!clang_isDeclaration(cursor.kind))
2467 return CXLinkage_Invalid;
2468
Ted Kremenek16b42592010-03-03 06:36:57 +00002469 Decl *D = cxcursor::getCursorDecl(cursor);
2470 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2471 switch (ND->getLinkage()) {
2472 case NoLinkage: return CXLinkage_NoLinkage;
2473 case InternalLinkage: return CXLinkage_Internal;
2474 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2475 case ExternalLinkage: return CXLinkage_External;
2476 };
2477
2478 return CXLinkage_Invalid;
2479}
2480} // end: extern "C"
2481
2482//===----------------------------------------------------------------------===//
Ted Kremenek45e1dae2010-04-12 21:22:16 +00002483// Operations for querying language of a cursor.
2484//===----------------------------------------------------------------------===//
2485
2486static CXLanguageKind getDeclLanguage(const Decl *D) {
2487 switch (D->getKind()) {
2488 default:
2489 break;
2490 case Decl::ImplicitParam:
2491 case Decl::ObjCAtDefsField:
2492 case Decl::ObjCCategory:
2493 case Decl::ObjCCategoryImpl:
2494 case Decl::ObjCClass:
2495 case Decl::ObjCCompatibleAlias:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00002496 case Decl::ObjCForwardProtocol:
2497 case Decl::ObjCImplementation:
2498 case Decl::ObjCInterface:
2499 case Decl::ObjCIvar:
2500 case Decl::ObjCMethod:
2501 case Decl::ObjCProperty:
2502 case Decl::ObjCPropertyImpl:
2503 case Decl::ObjCProtocol:
2504 return CXLanguage_ObjC;
2505 case Decl::CXXConstructor:
2506 case Decl::CXXConversion:
2507 case Decl::CXXDestructor:
2508 case Decl::CXXMethod:
2509 case Decl::CXXRecord:
2510 case Decl::ClassTemplate:
2511 case Decl::ClassTemplatePartialSpecialization:
2512 case Decl::ClassTemplateSpecialization:
2513 case Decl::Friend:
2514 case Decl::FriendTemplate:
2515 case Decl::FunctionTemplate:
2516 case Decl::LinkageSpec:
2517 case Decl::Namespace:
2518 case Decl::NamespaceAlias:
2519 case Decl::NonTypeTemplateParm:
2520 case Decl::StaticAssert:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00002521 case Decl::TemplateTemplateParm:
2522 case Decl::TemplateTypeParm:
2523 case Decl::UnresolvedUsingTypename:
2524 case Decl::UnresolvedUsingValue:
2525 case Decl::Using:
2526 case Decl::UsingDirective:
2527 case Decl::UsingShadow:
2528 return CXLanguage_CPlusPlus;
2529 }
2530
2531 return CXLanguage_C;
2532}
2533
2534extern "C" {
2535CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
2536 if (clang_isDeclaration(cursor.kind))
2537 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
2538
2539 return CXLanguage_Invalid;
2540}
2541} // end: extern "C"
2542
2543//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002544// CXString Operations.
2545//===----------------------------------------------------------------------===//
2546
2547extern "C" {
2548const char *clang_getCString(CXString string) {
2549 return string.Spelling;
2550}
2551
2552void clang_disposeString(CXString string) {
2553 if (string.MustFreeString && string.Spelling)
2554 free((void*)string.Spelling);
2555}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002556
Ted Kremenekfb480492010-01-13 21:46:36 +00002557} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002558
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002559namespace clang { namespace cxstring {
2560CXString createCXString(const char *String, bool DupString){
2561 CXString Str;
2562 if (DupString) {
2563 Str.Spelling = strdup(String);
2564 Str.MustFreeString = 1;
2565 } else {
2566 Str.Spelling = String;
2567 Str.MustFreeString = 0;
2568 }
2569 return Str;
2570}
2571
2572CXString createCXString(llvm::StringRef String, bool DupString) {
2573 CXString Result;
2574 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2575 char *Spelling = (char *)malloc(String.size() + 1);
2576 memmove(Spelling, String.data(), String.size());
2577 Spelling[String.size()] = 0;
2578 Result.Spelling = Spelling;
2579 Result.MustFreeString = 1;
2580 } else {
2581 Result.Spelling = String.data();
2582 Result.MustFreeString = 0;
2583 }
2584 return Result;
2585}
2586}}
2587
Ted Kremenek04bb7162010-01-22 22:44:15 +00002588//===----------------------------------------------------------------------===//
2589// Misc. utility functions.
2590//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002591
Ted Kremenek04bb7162010-01-22 22:44:15 +00002592extern "C" {
2593
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002594CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002595 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002596}
2597
2598} // end: extern "C"