blob: 5e7114d9efa345829c3608822257f107e80399dd [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);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000309 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000310};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000311
Ted Kremenekab188932010-01-05 19:32:54 +0000312} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000313
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000314RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000315 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
316}
317
Douglas Gregorb1373d02010-01-20 20:59:29 +0000318/// \brief Visit the given cursor and, if requested by the visitor,
319/// its children.
320///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000321/// \param Cursor the cursor to visit.
322///
323/// \param CheckRegionOfInterest if true, then the caller already checked that
324/// this cursor is within the region of interest.
325///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000326/// \returns true if the visitation should be aborted, false if it
327/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000328bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000329 if (clang_isInvalid(Cursor.kind))
330 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000331
Douglas Gregorb1373d02010-01-20 20:59:29 +0000332 if (clang_isDeclaration(Cursor.kind)) {
333 Decl *D = getCursorDecl(Cursor);
334 assert(D && "Invalid declaration cursor");
335 if (D->getPCHLevel() > MaxPCHLevel)
336 return false;
337
338 if (D->isImplicit())
339 return false;
340 }
341
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000342 // If we have a range of interest, and this cursor doesn't intersect with it,
343 // we're done.
344 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000345 SourceRange Range =
346 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
347 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000348 return false;
349 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000350
Douglas Gregorb1373d02010-01-20 20:59:29 +0000351 switch (Visitor(Cursor, Parent, ClientData)) {
352 case CXChildVisit_Break:
353 return true;
354
355 case CXChildVisit_Continue:
356 return false;
357
358 case CXChildVisit_Recurse:
359 return VisitChildren(Cursor);
360 }
361
Douglas Gregorfd643772010-01-25 16:45:46 +0000362 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000363}
364
Douglas Gregor788f5a12010-03-20 00:41:21 +0000365std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
366CursorVisitor::getPreprocessedEntities() {
367 PreprocessingRecord &PPRec
368 = *TU->getPreprocessor().getPreprocessingRecord();
369
370 bool OnlyLocalDecls
371 = !TU->isMainFileAST() && TU->getOnlyLocalDecls();
372
373 // There is no region of interest; we have to walk everything.
374 if (RegionOfInterest.isInvalid())
375 return std::make_pair(PPRec.begin(OnlyLocalDecls),
376 PPRec.end(OnlyLocalDecls));
377
378 // Find the file in which the region of interest lands.
379 SourceManager &SM = TU->getSourceManager();
380 std::pair<FileID, unsigned> Begin
381 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
382 std::pair<FileID, unsigned> End
383 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
384
385 // The region of interest spans files; we have to walk everything.
386 if (Begin.first != End.first)
387 return std::make_pair(PPRec.begin(OnlyLocalDecls),
388 PPRec.end(OnlyLocalDecls));
389
390 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
391 = TU->getPreprocessedEntitiesByFile();
392 if (ByFileMap.empty()) {
393 // Build the mapping from files to sets of preprocessed entities.
394 for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls),
395 EEnd = PPRec.end(OnlyLocalDecls);
396 E != EEnd; ++E) {
397 std::pair<FileID, unsigned> P
398 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
399 ByFileMap[P.first].push_back(*E);
400 }
401 }
402
403 return std::make_pair(ByFileMap[Begin.first].begin(),
404 ByFileMap[Begin.first].end());
405}
406
Douglas Gregorb1373d02010-01-20 20:59:29 +0000407/// \brief Visit the children of the given cursor.
408///
409/// \returns true if the visitation should be aborted, false if it
410/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000411bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000412 if (clang_isReference(Cursor.kind)) {
413 // By definition, references have no children.
414 return false;
415 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000416
417 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000418 // done.
419 class SetParentRAII {
420 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000421 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000422 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000423
Douglas Gregorb1373d02010-01-20 20:59:29 +0000424 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000425 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000426 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000427 {
428 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000429 if (clang_isDeclaration(Parent.kind))
430 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000431 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000432
Douglas Gregorb1373d02010-01-20 20:59:29 +0000433 ~SetParentRAII() {
434 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000435 if (clang_isDeclaration(Parent.kind))
436 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000437 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000438 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000439
Douglas Gregorb1373d02010-01-20 20:59:29 +0000440 if (clang_isDeclaration(Cursor.kind)) {
441 Decl *D = getCursorDecl(Cursor);
442 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000443 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000444 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000445
Douglas Gregora59e3902010-01-21 23:27:09 +0000446 if (clang_isStatement(Cursor.kind))
447 return Visit(getCursorStmt(Cursor));
448 if (clang_isExpression(Cursor.kind))
449 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000450
Douglas Gregorb1373d02010-01-20 20:59:29 +0000451 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000452 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000453 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
454 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000455 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
456 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
457 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000458 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000459 return true;
460 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000461 } else if (VisitDeclContext(
462 CXXUnit->getASTContext().getTranslationUnitDecl()))
463 return true;
Bob Wilson3178cb62010-03-19 03:57:57 +0000464
Douglas Gregor0396f462010-03-19 05:22:59 +0000465 // Walk the preprocessing record.
Daniel Dunbar8de30ff2010-03-20 01:11:56 +0000466 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000467 // FIXME: Once we have the ability to deserialize a preprocessing record,
468 // do so.
Douglas Gregor788f5a12010-03-20 00:41:21 +0000469 PreprocessingRecord::iterator E, EEnd;
470 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000471 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
472 if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit)))
473 return true;
Douglas Gregor788f5a12010-03-20 00:41:21 +0000474
Douglas Gregor0396f462010-03-19 05:22:59 +0000475 continue;
476 }
477
478 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
479 if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit)))
480 return true;
481
482 continue;
483 }
484 }
485 }
Douglas Gregor7b691f332010-01-20 21:13:59 +0000486 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000487 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000488
Douglas Gregorb1373d02010-01-20 20:59:29 +0000489 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000490 return false;
491}
492
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000493bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
494 for (BlockDecl::param_iterator I=B->param_begin(), E=B->param_end(); I!=E;++I)
495 if (Decl *D = *I)
496 if (Visit(D))
497 return true;
498
499 return Visit(MakeCXCursor(B->getBody(), StmtParent, TU));
500}
501
Douglas Gregorb1373d02010-01-20 20:59:29 +0000502bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000503 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000504 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek09dfa372010-02-18 05:46:33 +0000505
Daniel Dunbard52864b2010-02-14 10:02:57 +0000506 CXCursor Cursor = MakeCXCursor(*I, TU);
507
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000508 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000509 SourceRange Range =
510 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
511 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000512 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000513
514 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000515 case RangeBefore:
516 // This declaration comes before the region of interest; skip it.
517 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000518
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000519 case RangeAfter:
520 // This declaration comes after the region of interest; we're done.
521 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000522
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000523 case RangeOverlap:
524 // This declaration overlaps the region of interest; visit it.
525 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000526 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000527 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000528
Daniel Dunbard52864b2010-02-14 10:02:57 +0000529 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000530 return true;
531 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000532
Douglas Gregorb1373d02010-01-20 20:59:29 +0000533 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000534}
535
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000536bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
537 llvm_unreachable("Translation units are visited directly by Visit()");
538 return false;
539}
540
541bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
542 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
543 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000544
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000545 return false;
546}
547
548bool CursorVisitor::VisitTagDecl(TagDecl *D) {
549 return VisitDeclContext(D);
550}
551
552bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
553 if (Expr *Init = D->getInitExpr())
554 return Visit(MakeCXCursor(Init, StmtParent, TU));
555 return false;
556}
557
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000558bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
559 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
560 if (Visit(TSInfo->getTypeLoc()))
561 return true;
562
563 return false;
564}
565
Douglas Gregorb1373d02010-01-20 20:59:29 +0000566bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000567 if (VisitDeclaratorDecl(ND))
568 return true;
569
Douglas Gregora59e3902010-01-21 23:27:09 +0000570 if (ND->isThisDeclarationADefinition() &&
571 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
572 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000573
Douglas Gregorb1373d02010-01-20 20:59:29 +0000574 return false;
575}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000576
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000577bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
578 if (VisitDeclaratorDecl(D))
579 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000580
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000581 if (Expr *BitWidth = D->getBitWidth())
582 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000583
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000584 return false;
585}
586
587bool CursorVisitor::VisitVarDecl(VarDecl *D) {
588 if (VisitDeclaratorDecl(D))
589 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000590
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000591 if (Expr *Init = D->getInit())
592 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000593
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000594 return false;
595}
596
597bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000598 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
599 if (Visit(TSInfo->getTypeLoc()))
600 return true;
601
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000602 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000603 PEnd = ND->param_end();
604 P != PEnd; ++P) {
605 if (Visit(MakeCXCursor(*P, TU)))
606 return true;
607 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000608
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000609 if (ND->isThisDeclarationADefinition() &&
610 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
611 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000612
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000613 return false;
614}
615
Douglas Gregora59e3902010-01-21 23:27:09 +0000616bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
617 return VisitDeclContext(D);
618}
619
Douglas Gregorb1373d02010-01-20 20:59:29 +0000620bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000621 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
622 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000623 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000624
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000625 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
626 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
627 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000628 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000629 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000630
Douglas Gregora59e3902010-01-21 23:27:09 +0000631 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000632}
633
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000634bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
635 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
636 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
637 E = PID->protocol_end(); I != E; ++I, ++PL)
638 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
639 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000640
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000641 return VisitObjCContainerDecl(PID);
642}
643
Douglas Gregorb1373d02010-01-20 20:59:29 +0000644bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000645 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000646 if (D->getSuperClass() &&
647 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000648 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000649 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000650 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000651
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000652 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
653 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
654 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000655 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000656 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000657
Douglas Gregora59e3902010-01-21 23:27:09 +0000658 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000659}
660
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000661bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
662 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000663}
664
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000665bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000666 // 'ID' could be null when dealing with invalid code.
667 if (ObjCInterfaceDecl *ID = D->getClassInterface())
668 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
669 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000670
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000671 return VisitObjCImplDecl(D);
672}
673
674bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
675#if 0
676 // Issue callbacks for super class.
677 // FIXME: No source location information!
678 if (D->getSuperClass() &&
679 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000680 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000681 TU)))
682 return true;
683#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000684
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000685 return VisitObjCImplDecl(D);
686}
687
688bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
689 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
690 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
691 E = D->protocol_end();
692 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000693 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000694 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000695
696 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000697}
698
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000699bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
700 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
701 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
702 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000703
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000704 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000705}
706
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000707bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
708 ASTContext &Context = TU->getASTContext();
709
710 // Some builtin types (such as Objective-C's "id", "sel", and
711 // "Class") have associated declarations. Create cursors for those.
712 QualType VisitType;
713 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000714 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000715 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000716 case BuiltinType::Char_U:
717 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000718 case BuiltinType::Char16:
719 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000720 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000721 case BuiltinType::UInt:
722 case BuiltinType::ULong:
723 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000724 case BuiltinType::UInt128:
725 case BuiltinType::Char_S:
726 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000727 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000728 case BuiltinType::Short:
729 case BuiltinType::Int:
730 case BuiltinType::Long:
731 case BuiltinType::LongLong:
732 case BuiltinType::Int128:
733 case BuiltinType::Float:
734 case BuiltinType::Double:
735 case BuiltinType::LongDouble:
736 case BuiltinType::NullPtr:
737 case BuiltinType::Overload:
738 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000739 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000740
741 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000742 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000743
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000744 case BuiltinType::ObjCId:
745 VisitType = Context.getObjCIdType();
746 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000747
748 case BuiltinType::ObjCClass:
749 VisitType = Context.getObjCClassType();
750 break;
751
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000752 case BuiltinType::ObjCSel:
753 VisitType = Context.getObjCSelType();
754 break;
755 }
756
757 if (!VisitType.isNull()) {
758 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000759 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000760 TU));
761 }
762
763 return false;
764}
765
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000766bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
767 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
768}
769
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000770bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
771 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
772}
773
774bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
775 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
776}
777
778bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
779 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
780 return true;
781
782 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
783 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
784 TU)))
785 return true;
786 }
787
788 return false;
789}
790
791bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
792 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
793 return true;
794
795 if (TL.hasProtocolsAsWritten()) {
796 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000797 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000798 TL.getProtocolLoc(I),
799 TU)))
800 return true;
801 }
802 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000803
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000804 return false;
805}
806
807bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
808 return Visit(TL.getPointeeLoc());
809}
810
811bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
812 return Visit(TL.getPointeeLoc());
813}
814
815bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
816 return Visit(TL.getPointeeLoc());
817}
818
819bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000820 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000821}
822
823bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000824 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000825}
826
827bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
828 if (Visit(TL.getResultLoc()))
829 return true;
830
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000831 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
Ted Kremenek5dbacb42010-04-07 00:27:13 +0000832 if (Decl *D = TL.getArg(I))
833 if (Visit(MakeCXCursor(D, TU)))
834 return true;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000835
836 return false;
837}
838
839bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
840 if (Visit(TL.getElementLoc()))
841 return true;
842
843 if (Expr *Size = TL.getSizeExpr())
844 return Visit(MakeCXCursor(Size, StmtParent, TU));
845
846 return false;
847}
848
Douglas Gregor2332c112010-01-21 20:48:56 +0000849bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
850 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
851}
852
853bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
854 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
855 return Visit(TSInfo->getTypeLoc());
856
857 return false;
858}
859
Douglas Gregora59e3902010-01-21 23:27:09 +0000860bool CursorVisitor::VisitStmt(Stmt *S) {
861 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
862 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000863 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000864 return true;
865 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000866
Douglas Gregora59e3902010-01-21 23:27:09 +0000867 return false;
868}
869
870bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
871 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
872 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000873 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000874 return true;
875 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000876
Douglas Gregora59e3902010-01-21 23:27:09 +0000877 return false;
878}
879
Douglas Gregorf5bab412010-01-22 01:00:11 +0000880bool CursorVisitor::VisitIfStmt(IfStmt *S) {
881 if (VarDecl *Var = S->getConditionVariable()) {
882 if (Visit(MakeCXCursor(Var, TU)))
883 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000884 }
885
Douglas Gregor263b47b2010-01-25 16:12:32 +0000886 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
887 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000888 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
889 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000890 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
891 return true;
892
893 return false;
894}
895
896bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
897 if (VarDecl *Var = S->getConditionVariable()) {
898 if (Visit(MakeCXCursor(Var, TU)))
899 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000900 }
901
Douglas Gregor263b47b2010-01-25 16:12:32 +0000902 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
903 return true;
904 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
905 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000906
Douglas Gregor263b47b2010-01-25 16:12:32 +0000907 return false;
908}
909
910bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
911 if (VarDecl *Var = S->getConditionVariable()) {
912 if (Visit(MakeCXCursor(Var, TU)))
913 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000914 }
915
Douglas Gregor263b47b2010-01-25 16:12:32 +0000916 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
917 return true;
918 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000919 return true;
920
Douglas Gregor263b47b2010-01-25 16:12:32 +0000921 return false;
922}
923
924bool CursorVisitor::VisitForStmt(ForStmt *S) {
925 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
926 return true;
927 if (VarDecl *Var = S->getConditionVariable()) {
928 if (Visit(MakeCXCursor(Var, TU)))
929 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000930 }
931
Douglas Gregor263b47b2010-01-25 16:12:32 +0000932 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
933 return true;
934 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
935 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000936 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
937 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000938
Douglas Gregorf5bab412010-01-22 01:00:11 +0000939 return false;
940}
941
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000942bool CursorVisitor::VisitBlockExpr(BlockExpr *B) {
943 return Visit(B->getBlockDecl());
944}
945
Douglas Gregor336fd812010-01-23 00:40:08 +0000946bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
947 if (E->isArgumentType()) {
948 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
949 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000950
Douglas Gregor336fd812010-01-23 00:40:08 +0000951 return false;
952 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000953
Douglas Gregor336fd812010-01-23 00:40:08 +0000954 return VisitExpr(E);
955}
956
957bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
958 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
959 if (Visit(TSInfo->getTypeLoc()))
960 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000961
Douglas Gregor336fd812010-01-23 00:40:08 +0000962 return VisitCastExpr(E);
963}
964
965bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
966 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
967 if (Visit(TSInfo->getTypeLoc()))
968 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000969
Douglas Gregor336fd812010-01-23 00:40:08 +0000970 return VisitExpr(E);
971}
972
Douglas Gregorc2350e52010-03-08 16:40:19 +0000973bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Douglas Gregor04badcf2010-04-21 00:45:42 +0000974 if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo())
975 if (Visit(TSInfo->getTypeLoc()))
976 return true;
Douglas Gregorc2350e52010-03-08 16:40:19 +0000977
978 return VisitExpr(E);
979}
980
Douglas Gregor81d34662010-04-20 15:39:42 +0000981bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
982 return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc());
983}
984
985
Ted Kremenek09dfa372010-02-18 05:46:33 +0000986bool CursorVisitor::VisitAttributes(Decl *D) {
987 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
988 if (Visit(MakeCXCursor(A, D, TU)))
989 return true;
990
991 return false;
992}
993
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000994extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000995CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
996 int displayDiagnostics) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000997 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000998 if (excludeDeclarationsFromPCH)
999 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001000 if (displayDiagnostics)
1001 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001002 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +00001003}
1004
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001005void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001006 if (CIdx)
1007 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001008}
1009
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001010void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001011 if (CIdx) {
1012 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
1013 CXXIdx->setUseExternalASTGeneration(value);
1014 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001015}
1016
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001017CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +00001018 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001019 if (!CIdx)
1020 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001021
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00001022 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001023
Douglas Gregor28019772010-04-05 23:52:57 +00001024 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
1025 return ASTUnit::LoadFromPCHFile(ast_filename, Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +00001026 CXXIdx->getOnlyLocalDecls(),
1027 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +00001028}
1029
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001030CXTranslationUnit
1031clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
1032 const char *source_filename,
1033 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001034 const char **command_line_args,
1035 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001036 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001037 if (!CIdx)
1038 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001039
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001040 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
1041
Douglas Gregor5352ac02010-01-28 00:27:43 +00001042 // Configure the diagnostics.
1043 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +00001044 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
1045 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001046
Douglas Gregor4db64a42010-01-23 00:14:00 +00001047 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
1048 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattnera0a270c2010-04-05 22:42:27 +00001049 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001050 const llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001051 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001052 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
1053 Buffer));
1054 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001055
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001056 if (!CXXIdx->getUseExternalASTGeneration()) {
1057 llvm::SmallVector<const char *, 16> Args;
1058
1059 // The 'source_filename' argument is optional. If the caller does not
1060 // specify it then it is assumed that the source file is specified
1061 // in the actual argument list.
1062 if (source_filename)
1063 Args.push_back(source_filename);
1064 Args.insert(Args.end(), command_line_args,
1065 command_line_args + num_command_line_args);
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001066 Args.push_back("-Xclang");
1067 Args.push_back("-detailed-preprocessing-record");
Douglas Gregor5352ac02010-01-28 00:27:43 +00001068 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001069
Ted Kremenek29b72842010-01-07 22:49:05 +00001070#ifdef USE_CRASHTRACER
1071 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +00001072#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001073
Daniel Dunbar94220972009-12-05 02:17:18 +00001074 llvm::OwningPtr<ASTUnit> Unit(
1075 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +00001076 Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +00001077 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +00001078 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001079 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001080 RemappedFiles.size(),
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001081 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001082
Daniel Dunbar94220972009-12-05 02:17:18 +00001083 // FIXME: Until we have broader testing, just drop the entire AST if we
1084 // encountered an error.
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001085 if (NumErrors != Diags->getNumErrors()) {
Ted Kremenek34f6a322010-03-05 22:43:29 +00001086 // Make sure to check that 'Unit' is non-NULL.
1087 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
Douglas Gregor405634b2010-04-05 18:10:21 +00001088 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
1089 DEnd = Unit->stored_diag_end();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001090 D != DEnd; ++D) {
1091 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001092 CXString Msg = clang_formatDiagnostic(&Diag,
1093 clang_defaultDiagnosticDisplayOptions());
1094 fprintf(stderr, "%s\n", clang_getCString(Msg));
1095 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001096 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001097#ifdef LLVM_ON_WIN32
1098 // On Windows, force a flush, since there may be multiple copies of
1099 // stderr and stdout in the file system, all with different buffers
1100 // but writing to the same device.
1101 fflush(stderr);
Ted Kremenek83c51842010-03-26 01:34:51 +00001102#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001103 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001104 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001105
1106 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001107 }
1108
Ted Kremenek139ba862009-10-22 00:03:57 +00001109 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001110 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001111
Ted Kremenek139ba862009-10-22 00:03:57 +00001112 // First add the complete path to the 'clang' executable.
1113 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001114 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001115
Ted Kremenek139ba862009-10-22 00:03:57 +00001116 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001117 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001118
Ted Kremenek139ba862009-10-22 00:03:57 +00001119 // The 'source_filename' argument is optional. If the caller does not
1120 // specify it then it is assumed that the source file is specified
1121 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001122 if (source_filename)
1123 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001124
Steve Naroff37b5ac22009-10-15 20:50:09 +00001125 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001126 argv.push_back("-o");
Benjamin Kramerc2a98162010-03-13 21:22:49 +00001127 char astTmpFile[L_tmpnam];
1128 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001129
Douglas Gregor4db64a42010-01-23 00:14:00 +00001130 // Remap any unsaved files to temporary files.
1131 std::vector<llvm::sys::Path> TemporaryFiles;
1132 std::vector<std::string> RemapArgs;
1133 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1134 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001135
Douglas Gregor4db64a42010-01-23 00:14:00 +00001136 // The pointers into the elements of RemapArgs are stable because we
1137 // won't be adding anything to RemapArgs after this point.
1138 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1139 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001140
Ted Kremenek139ba862009-10-22 00:03:57 +00001141 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1142 for (int i = 0; i < num_command_line_args; ++i)
1143 if (const char *arg = command_line_args[i]) {
1144 if (strcmp(arg, "-o") == 0) {
1145 ++i; // Also skip the matching argument.
1146 continue;
1147 }
1148 if (strcmp(arg, "-emit-ast") == 0 ||
1149 strcmp(arg, "-c") == 0 ||
1150 strcmp(arg, "-fsyntax-only") == 0) {
1151 continue;
1152 }
1153
1154 // Keep the argument.
1155 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001156 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001157
Douglas Gregord93256e2010-01-28 06:00:51 +00001158 // Generate a temporary name for the diagnostics file.
Benjamin Kramerc2a98162010-03-13 21:22:49 +00001159 char tmpFileResults[L_tmpnam];
1160 char *tmpResultsFileName = tmpnam(tmpFileResults);
1161 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
Douglas Gregord93256e2010-01-28 06:00:51 +00001162 TemporaryFiles.push_back(DiagnosticsFile);
1163 argv.push_back("-fdiagnostics-binary");
1164
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001165 argv.push_back("-Xclang");
1166 argv.push_back("-detailed-preprocessing-record");
1167
Ted Kremenek139ba862009-10-22 00:03:57 +00001168 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001169 argv.push_back(NULL);
1170
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001171 // Invoke 'clang'.
1172 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1173 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001174 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001175 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1176 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001177 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001178 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001179 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001180
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001181 if (!ErrMsg.empty()) {
1182 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001183 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001184 I != E; ++I) {
1185 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001186 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001187 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001188 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001189
Daniel Dunbar32141c82010-02-23 20:23:45 +00001190 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001191 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001192
Douglas Gregor3687e9d2010-04-05 21:10:19 +00001193 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001194 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001195 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001196 RemappedFiles.size(),
1197 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001198 if (ATU) {
1199 LoadSerializedDiagnostics(DiagnosticsFile,
1200 num_unsaved_files, unsaved_files,
1201 ATU->getFileManager(),
1202 ATU->getSourceManager(),
Douglas Gregor405634b2010-04-05 18:10:21 +00001203 ATU->getStoredDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001204 } else if (CXXIdx->getDisplayDiagnostics()) {
1205 // We failed to load the ASTUnit, but we can still deserialize the
1206 // diagnostics and emit them.
1207 FileManager FileMgr;
Douglas Gregorf715ca12010-03-16 00:06:06 +00001208 Diagnostic Diag;
1209 SourceManager SourceMgr(Diag);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001210 // FIXME: Faked LangOpts!
1211 LangOptions LangOpts;
1212 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1213 LoadSerializedDiagnostics(DiagnosticsFile,
1214 num_unsaved_files, unsaved_files,
1215 FileMgr, SourceMgr, Diags);
1216 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1217 DEnd = Diags.end();
1218 D != DEnd; ++D) {
1219 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001220 CXString Msg = clang_formatDiagnostic(&Diag,
1221 clang_defaultDiagnosticDisplayOptions());
1222 fprintf(stderr, "%s\n", clang_getCString(Msg));
1223 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001224 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001225
1226#ifdef LLVM_ON_WIN32
1227 // On Windows, force a flush, since there may be multiple copies of
1228 // stderr and stdout in the file system, all with different buffers
1229 // but writing to the same device.
1230 fflush(stderr);
1231#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001232 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001233
Douglas Gregor313e26c2010-02-18 23:35:40 +00001234 if (ATU) {
1235 // Make the translation unit responsible for destroying all temporary files.
1236 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1237 ATU->addTemporaryFile(TemporaryFiles[i]);
1238 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1239 } else {
1240 // Destroy all of the temporary files now; they can't be referenced any
1241 // longer.
1242 llvm::sys::Path(astTmpFile).eraseFromDisk();
1243 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1244 TemporaryFiles[i].eraseFromDisk();
1245 }
1246
Steve Naroffe19944c2009-10-15 22:23:48 +00001247 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001248}
1249
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001250void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001251 if (CTUnit)
1252 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001253}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001254
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001255CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001256 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001257 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001258
Steve Naroff77accc12009-09-03 18:19:54 +00001259 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001260 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001261}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001262
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001263CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001264 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001265 return Result;
1266}
1267
Ted Kremenekfb480492010-01-13 21:46:36 +00001268} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001269
Ted Kremenekfb480492010-01-13 21:46:36 +00001270//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001271// CXSourceLocation and CXSourceRange Operations.
1272//===----------------------------------------------------------------------===//
1273
Douglas Gregorb9790342010-01-22 21:44:22 +00001274extern "C" {
1275CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001276 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001277 return Result;
1278}
1279
1280unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001281 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1282 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1283 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001284}
1285
1286CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1287 CXFile file,
1288 unsigned line,
1289 unsigned column) {
1290 if (!tu)
1291 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001292
Douglas Gregorb9790342010-01-22 21:44:22 +00001293 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1294 SourceLocation SLoc
1295 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001296 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001297 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001298
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001299 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001300}
1301
Douglas Gregor5352ac02010-01-28 00:27:43 +00001302CXSourceRange clang_getNullRange() {
1303 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1304 return Result;
1305}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001306
Douglas Gregor5352ac02010-01-28 00:27:43 +00001307CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1308 if (begin.ptr_data[0] != end.ptr_data[0] ||
1309 begin.ptr_data[1] != end.ptr_data[1])
1310 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001311
1312 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001313 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001314 return Result;
1315}
1316
Douglas Gregor46766dc2010-01-26 19:19:08 +00001317void clang_getInstantiationLocation(CXSourceLocation location,
1318 CXFile *file,
1319 unsigned *line,
1320 unsigned *column,
1321 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001322 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1323
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001324 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001325 if (file)
1326 *file = 0;
1327 if (line)
1328 *line = 0;
1329 if (column)
1330 *column = 0;
1331 if (offset)
1332 *offset = 0;
1333 return;
1334 }
1335
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001336 const SourceManager &SM =
1337 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001338 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001339
1340 if (file)
1341 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1342 if (line)
1343 *line = SM.getInstantiationLineNumber(InstLoc);
1344 if (column)
1345 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001346 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001347 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001348}
1349
Douglas Gregor1db19de2010-01-19 21:36:55 +00001350CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001351 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001352 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001353 return Result;
1354}
1355
1356CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001357 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001358 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001359 return Result;
1360}
1361
Douglas Gregorb9790342010-01-22 21:44:22 +00001362} // end: extern "C"
1363
Douglas Gregor1db19de2010-01-19 21:36:55 +00001364//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001365// CXFile Operations.
1366//===----------------------------------------------------------------------===//
1367
1368extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001369CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001370 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001371 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001372
Steve Naroff88145032009-10-27 14:35:18 +00001373 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001374 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001375}
1376
1377time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001378 if (!SFile)
1379 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001380
Steve Naroff88145032009-10-27 14:35:18 +00001381 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1382 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001383}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001384
Douglas Gregorb9790342010-01-22 21:44:22 +00001385CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1386 if (!tu)
1387 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001388
Douglas Gregorb9790342010-01-22 21:44:22 +00001389 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001390
Douglas Gregorb9790342010-01-22 21:44:22 +00001391 FileManager &FMgr = CXXUnit->getFileManager();
1392 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1393 return const_cast<FileEntry *>(File);
1394}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001395
Ted Kremenekfb480492010-01-13 21:46:36 +00001396} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001397
Ted Kremenekfb480492010-01-13 21:46:36 +00001398//===----------------------------------------------------------------------===//
1399// CXCursor Operations.
1400//===----------------------------------------------------------------------===//
1401
Ted Kremenekfb480492010-01-13 21:46:36 +00001402static Decl *getDeclFromExpr(Stmt *E) {
1403 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1404 return RefExpr->getDecl();
1405 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1406 return ME->getMemberDecl();
1407 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1408 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001409
Ted Kremenekfb480492010-01-13 21:46:36 +00001410 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1411 return getDeclFromExpr(CE->getCallee());
1412 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1413 return getDeclFromExpr(CE->getSubExpr());
1414 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1415 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001416
Ted Kremenekfb480492010-01-13 21:46:36 +00001417 return 0;
1418}
1419
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001420static SourceLocation getLocationFromExpr(Expr *E) {
1421 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1422 return /*FIXME:*/Msg->getLeftLoc();
1423 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1424 return DRE->getLocation();
1425 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1426 return Member->getMemberLoc();
1427 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1428 return Ivar->getLocation();
1429 return E->getLocStart();
1430}
1431
Ted Kremenekfb480492010-01-13 21:46:36 +00001432extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001433
1434unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001435 CXCursorVisitor visitor,
1436 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001437 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001438
1439 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001440
Douglas Gregorb1373d02010-01-20 20:59:29 +00001441 // Set the PCHLevel to filter out unwanted decls if requested.
1442 if (CXXUnit->getOnlyLocalDecls()) {
1443 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001444
Douglas Gregorb1373d02010-01-20 20:59:29 +00001445 // If the main input was an AST, bump the level.
1446 if (CXXUnit->isMainFileAST())
1447 ++PCHLevel;
1448 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001449
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001450 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001451 return CursorVis.VisitChildren(parent);
1452}
1453
Douglas Gregor78205d42010-01-20 21:45:58 +00001454static CXString getDeclSpelling(Decl *D) {
1455 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1456 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001457 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001458
Douglas Gregor78205d42010-01-20 21:45:58 +00001459 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001460 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001461
Douglas Gregor78205d42010-01-20 21:45:58 +00001462 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1463 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1464 // and returns different names. NamedDecl returns the class name and
1465 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001466 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001467
Douglas Gregor78205d42010-01-20 21:45:58 +00001468 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001469 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001470
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001471 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001472}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001473
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001474CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001475 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001476 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001477
Steve Narofff334b4e2009-09-02 18:26:48 +00001478 if (clang_isReference(C.kind)) {
1479 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001480 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001481 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001482 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001483 }
1484 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001485 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001486 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001487 }
1488 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001489 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001490 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001491 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001492 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001493 case CXCursor_TypeRef: {
1494 TypeDecl *Type = getCursorTypeRef(C).first;
1495 assert(Type && "Missing type decl");
1496
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001497 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1498 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001499 }
1500
Daniel Dunbaracca7252009-11-30 20:42:49 +00001501 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001502 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001503 }
1504 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001505
1506 if (clang_isExpression(C.kind)) {
1507 Decl *D = getDeclFromExpr(getCursorExpr(C));
1508 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001509 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001510 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001511 }
1512
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001513 if (C.kind == CXCursor_MacroInstantiation)
1514 return createCXString(getCursorMacroInstantiation(C)->getName()
1515 ->getNameStart());
1516
Douglas Gregor572feb22010-03-18 18:04:21 +00001517 if (C.kind == CXCursor_MacroDefinition)
1518 return createCXString(getCursorMacroDefinition(C)->getName()
1519 ->getNameStart());
1520
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001521 if (clang_isDeclaration(C.kind))
1522 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001523
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001524 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001525}
1526
Ted Kremeneke68fff62010-02-17 00:41:32 +00001527CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001528 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001529 case CXCursor_FunctionDecl:
1530 return createCXString("FunctionDecl");
1531 case CXCursor_TypedefDecl:
1532 return createCXString("TypedefDecl");
1533 case CXCursor_EnumDecl:
1534 return createCXString("EnumDecl");
1535 case CXCursor_EnumConstantDecl:
1536 return createCXString("EnumConstantDecl");
1537 case CXCursor_StructDecl:
1538 return createCXString("StructDecl");
1539 case CXCursor_UnionDecl:
1540 return createCXString("UnionDecl");
1541 case CXCursor_ClassDecl:
1542 return createCXString("ClassDecl");
1543 case CXCursor_FieldDecl:
1544 return createCXString("FieldDecl");
1545 case CXCursor_VarDecl:
1546 return createCXString("VarDecl");
1547 case CXCursor_ParmDecl:
1548 return createCXString("ParmDecl");
1549 case CXCursor_ObjCInterfaceDecl:
1550 return createCXString("ObjCInterfaceDecl");
1551 case CXCursor_ObjCCategoryDecl:
1552 return createCXString("ObjCCategoryDecl");
1553 case CXCursor_ObjCProtocolDecl:
1554 return createCXString("ObjCProtocolDecl");
1555 case CXCursor_ObjCPropertyDecl:
1556 return createCXString("ObjCPropertyDecl");
1557 case CXCursor_ObjCIvarDecl:
1558 return createCXString("ObjCIvarDecl");
1559 case CXCursor_ObjCInstanceMethodDecl:
1560 return createCXString("ObjCInstanceMethodDecl");
1561 case CXCursor_ObjCClassMethodDecl:
1562 return createCXString("ObjCClassMethodDecl");
1563 case CXCursor_ObjCImplementationDecl:
1564 return createCXString("ObjCImplementationDecl");
1565 case CXCursor_ObjCCategoryImplDecl:
1566 return createCXString("ObjCCategoryImplDecl");
Ted Kremenek8bd5a692010-04-13 23:39:06 +00001567 case CXCursor_CXXMethod:
1568 return createCXString("CXXMethod");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001569 case CXCursor_UnexposedDecl:
1570 return createCXString("UnexposedDecl");
1571 case CXCursor_ObjCSuperClassRef:
1572 return createCXString("ObjCSuperClassRef");
1573 case CXCursor_ObjCProtocolRef:
1574 return createCXString("ObjCProtocolRef");
1575 case CXCursor_ObjCClassRef:
1576 return createCXString("ObjCClassRef");
1577 case CXCursor_TypeRef:
1578 return createCXString("TypeRef");
1579 case CXCursor_UnexposedExpr:
1580 return createCXString("UnexposedExpr");
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00001581 case CXCursor_BlockExpr:
1582 return createCXString("BlockExpr");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001583 case CXCursor_DeclRefExpr:
1584 return createCXString("DeclRefExpr");
1585 case CXCursor_MemberRefExpr:
1586 return createCXString("MemberRefExpr");
1587 case CXCursor_CallExpr:
1588 return createCXString("CallExpr");
1589 case CXCursor_ObjCMessageExpr:
1590 return createCXString("ObjCMessageExpr");
1591 case CXCursor_UnexposedStmt:
1592 return createCXString("UnexposedStmt");
1593 case CXCursor_InvalidFile:
1594 return createCXString("InvalidFile");
Ted Kremenek292db642010-03-19 20:39:05 +00001595 case CXCursor_InvalidCode:
1596 return createCXString("InvalidCode");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001597 case CXCursor_NoDeclFound:
1598 return createCXString("NoDeclFound");
1599 case CXCursor_NotImplemented:
1600 return createCXString("NotImplemented");
1601 case CXCursor_TranslationUnit:
1602 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001603 case CXCursor_UnexposedAttr:
1604 return createCXString("UnexposedAttr");
1605 case CXCursor_IBActionAttr:
1606 return createCXString("attribute(ibaction)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001607 case CXCursor_IBOutletAttr:
1608 return createCXString("attribute(iboutlet)");
1609 case CXCursor_PreprocessingDirective:
1610 return createCXString("preprocessing directive");
Douglas Gregor572feb22010-03-18 18:04:21 +00001611 case CXCursor_MacroDefinition:
1612 return createCXString("macro definition");
Douglas Gregor48072312010-03-18 15:23:44 +00001613 case CXCursor_MacroInstantiation:
1614 return createCXString("macro instantiation");
Steve Naroff89922f82009-08-31 00:59:03 +00001615 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001616
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001617 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001618 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001619}
Steve Naroff89922f82009-08-31 00:59:03 +00001620
Ted Kremeneke68fff62010-02-17 00:41:32 +00001621enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1622 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001623 CXClientData client_data) {
1624 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1625 *BestCursor = cursor;
1626 return CXChildVisit_Recurse;
1627}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001628
Douglas Gregorb9790342010-01-22 21:44:22 +00001629CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1630 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001631 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001632
Douglas Gregorb9790342010-01-22 21:44:22 +00001633 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1634
Douglas Gregorbdf60622010-03-05 21:16:25 +00001635 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1636
Ted Kremeneka297de22010-01-25 22:34:44 +00001637 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001638 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1639 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001640 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001641
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001642 // FIXME: Would be great to have a "hint" cursor, then walk from that
1643 // hint cursor upward until we find a cursor whose source range encloses
1644 // the region of interest, rather than starting from the translation unit.
1645 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001646 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001647 Decl::MaxPCHLevel, RegionOfInterest);
1648 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001649 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001650 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001651}
1652
Ted Kremenek73885552009-11-17 19:28:59 +00001653CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001654 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001655}
1656
1657unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001658 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001659}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001660
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001661unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001662 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1663}
1664
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001665unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001666 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1667}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001668
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001669unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001670 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1671}
1672
Douglas Gregor97b98722010-01-19 23:20:36 +00001673unsigned clang_isExpression(enum CXCursorKind K) {
1674 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1675}
1676
1677unsigned clang_isStatement(enum CXCursorKind K) {
1678 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1679}
1680
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001681unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1682 return K == CXCursor_TranslationUnit;
1683}
1684
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001685unsigned clang_isPreprocessing(enum CXCursorKind K) {
1686 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
1687}
1688
Ted Kremenekad6eff62010-03-08 21:17:29 +00001689unsigned clang_isUnexposed(enum CXCursorKind K) {
1690 switch (K) {
1691 case CXCursor_UnexposedDecl:
1692 case CXCursor_UnexposedExpr:
1693 case CXCursor_UnexposedStmt:
1694 case CXCursor_UnexposedAttr:
1695 return true;
1696 default:
1697 return false;
1698 }
1699}
1700
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001701CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001702 return C.kind;
1703}
1704
Douglas Gregor98258af2010-01-18 22:46:11 +00001705CXSourceLocation clang_getCursorLocation(CXCursor C) {
1706 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001707 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001708 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001709 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1710 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001711 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001712 }
1713
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001714 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001715 std::pair<ObjCProtocolDecl *, SourceLocation> P
1716 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001717 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001718 }
1719
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001720 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001721 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1722 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001723 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001724 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001725
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001726 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001727 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001728 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001729 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001730
Douglas Gregorf46034a2010-01-18 23:41:10 +00001731 default:
1732 // FIXME: Need a way to enumerate all non-reference cases.
1733 llvm_unreachable("Missed a reference kind");
1734 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001735 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001736
1737 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001738 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001739 getLocationFromExpr(getCursorExpr(C)));
1740
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001741 if (C.kind == CXCursor_PreprocessingDirective) {
1742 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
1743 return cxloc::translateSourceLocation(getCursorContext(C), L);
1744 }
Douglas Gregor48072312010-03-18 15:23:44 +00001745
1746 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001747 SourceLocation L
1748 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
Douglas Gregor48072312010-03-18 15:23:44 +00001749 return cxloc::translateSourceLocation(getCursorContext(C), L);
1750 }
Douglas Gregor572feb22010-03-18 18:04:21 +00001751
1752 if (C.kind == CXCursor_MacroDefinition) {
1753 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
1754 return cxloc::translateSourceLocation(getCursorContext(C), L);
1755 }
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001756
Douglas Gregor5352ac02010-01-28 00:27:43 +00001757 if (!getCursorDecl(C))
1758 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001759
Douglas Gregorf46034a2010-01-18 23:41:10 +00001760 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001761 SourceLocation Loc = D->getLocation();
1762 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1763 Loc = Class->getClassLoc();
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00001764 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001765}
Douglas Gregora7bde202010-01-19 00:34:46 +00001766
1767CXSourceRange clang_getCursorExtent(CXCursor C) {
1768 if (clang_isReference(C.kind)) {
1769 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001770 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001771 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1772 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001773 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001774 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001775
1776 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001777 std::pair<ObjCProtocolDecl *, SourceLocation> P
1778 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001779 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001780 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001781
1782 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001783 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1784 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001785
Ted Kremeneka297de22010-01-25 22:34:44 +00001786 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001787 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001788
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001789 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001790 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001791 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001792 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001793
Douglas Gregora7bde202010-01-19 00:34:46 +00001794 default:
1795 // FIXME: Need a way to enumerate all non-reference cases.
1796 llvm_unreachable("Missed a reference kind");
1797 }
1798 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001799
1800 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001801 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001802 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001803
1804 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001805 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001806 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001807
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001808 if (C.kind == CXCursor_PreprocessingDirective) {
1809 SourceRange R = cxcursor::getCursorPreprocessingDirective(C);
1810 return cxloc::translateSourceRange(getCursorContext(C), R);
1811 }
Douglas Gregor48072312010-03-18 15:23:44 +00001812
1813 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00001814 SourceRange R = cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +00001815 return cxloc::translateSourceRange(getCursorContext(C), R);
1816 }
Douglas Gregor572feb22010-03-18 18:04:21 +00001817
1818 if (C.kind == CXCursor_MacroDefinition) {
1819 SourceRange R = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
1820 return cxloc::translateSourceRange(getCursorContext(C), R);
1821 }
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00001822
Douglas Gregor5352ac02010-01-28 00:27:43 +00001823 if (!getCursorDecl(C))
1824 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001825
Douglas Gregora7bde202010-01-19 00:34:46 +00001826 Decl *D = getCursorDecl(C);
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00001827 return cxloc::translateSourceRange(getCursorContext(C), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001828}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001829
1830CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001831 if (clang_isInvalid(C.kind))
1832 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001833
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001834 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001835 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001836 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001837
Douglas Gregor97b98722010-01-19 23:20:36 +00001838 if (clang_isExpression(C.kind)) {
1839 Decl *D = getDeclFromExpr(getCursorExpr(C));
1840 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001841 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001842 return clang_getNullCursor();
1843 }
1844
Douglas Gregorbf7efa22010-03-18 18:23:03 +00001845 if (C.kind == CXCursor_MacroInstantiation) {
1846 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
1847 return MakeMacroDefinitionCursor(Def, CXXUnit);
1848 }
1849
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001850 if (!clang_isReference(C.kind))
1851 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001852
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001853 switch (C.kind) {
1854 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001855 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001856
1857 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001858 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001859
1860 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001861 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001862
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001863 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001864 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001865
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001866 default:
1867 // We would prefer to enumerate all non-reference cursor kinds here.
1868 llvm_unreachable("Unhandled reference cursor kind");
1869 break;
1870 }
1871 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001872
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001873 return clang_getNullCursor();
1874}
1875
Douglas Gregorb6998662010-01-19 19:34:47 +00001876CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001877 if (clang_isInvalid(C.kind))
1878 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001879
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001880 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001881
Douglas Gregorb6998662010-01-19 19:34:47 +00001882 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001883 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001884 C = clang_getCursorReferenced(C);
1885 WasReference = true;
1886 }
1887
Douglas Gregorbf7efa22010-03-18 18:23:03 +00001888 if (C.kind == CXCursor_MacroInstantiation)
1889 return clang_getCursorReferenced(C);
1890
Douglas Gregorb6998662010-01-19 19:34:47 +00001891 if (!clang_isDeclaration(C.kind))
1892 return clang_getNullCursor();
1893
1894 Decl *D = getCursorDecl(C);
1895 if (!D)
1896 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001897
Douglas Gregorb6998662010-01-19 19:34:47 +00001898 switch (D->getKind()) {
1899 // Declaration kinds that don't really separate the notions of
1900 // declaration and definition.
1901 case Decl::Namespace:
1902 case Decl::Typedef:
1903 case Decl::TemplateTypeParm:
1904 case Decl::EnumConstant:
1905 case Decl::Field:
1906 case Decl::ObjCIvar:
1907 case Decl::ObjCAtDefsField:
1908 case Decl::ImplicitParam:
1909 case Decl::ParmVar:
1910 case Decl::NonTypeTemplateParm:
1911 case Decl::TemplateTemplateParm:
1912 case Decl::ObjCCategoryImpl:
1913 case Decl::ObjCImplementation:
1914 case Decl::LinkageSpec:
1915 case Decl::ObjCPropertyImpl:
1916 case Decl::FileScopeAsm:
1917 case Decl::StaticAssert:
1918 case Decl::Block:
1919 return C;
1920
1921 // Declaration kinds that don't make any sense here, but are
1922 // nonetheless harmless.
1923 case Decl::TranslationUnit:
1924 case Decl::Template:
1925 case Decl::ObjCContainer:
1926 break;
1927
1928 // Declaration kinds for which the definition is not resolvable.
1929 case Decl::UnresolvedUsingTypename:
1930 case Decl::UnresolvedUsingValue:
1931 break;
1932
1933 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001934 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1935 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001936
1937 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001938 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001939
1940 case Decl::Enum:
1941 case Decl::Record:
1942 case Decl::CXXRecord:
1943 case Decl::ClassTemplateSpecialization:
1944 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001945 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001946 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001947 return clang_getNullCursor();
1948
1949 case Decl::Function:
1950 case Decl::CXXMethod:
1951 case Decl::CXXConstructor:
1952 case Decl::CXXDestructor:
1953 case Decl::CXXConversion: {
1954 const FunctionDecl *Def = 0;
1955 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001956 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001957 return clang_getNullCursor();
1958 }
1959
1960 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001961 // Ask the variable if it has a definition.
1962 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1963 return MakeCXCursor(Def, CXXUnit);
1964 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001965 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001966
Douglas Gregorb6998662010-01-19 19:34:47 +00001967 case Decl::FunctionTemplate: {
1968 const FunctionDecl *Def = 0;
1969 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001970 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001971 return clang_getNullCursor();
1972 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001973
Douglas Gregorb6998662010-01-19 19:34:47 +00001974 case Decl::ClassTemplate: {
1975 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001976 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001977 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001978 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001979 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001980 return clang_getNullCursor();
1981 }
1982
1983 case Decl::Using: {
1984 UsingDecl *Using = cast<UsingDecl>(D);
1985 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001986 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1987 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001988 S != SEnd; ++S) {
1989 if (Def != clang_getNullCursor()) {
1990 // FIXME: We have no way to return multiple results.
1991 return clang_getNullCursor();
1992 }
1993
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001994 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001995 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001996 }
1997
1998 return Def;
1999 }
2000
2001 case Decl::UsingShadow:
2002 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002003 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002004 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002005
2006 case Decl::ObjCMethod: {
2007 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
2008 if (Method->isThisDeclarationADefinition())
2009 return C;
2010
2011 // Dig out the method definition in the associated
2012 // @implementation, if we have it.
2013 // FIXME: The ASTs should make finding the definition easier.
2014 if (ObjCInterfaceDecl *Class
2015 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
2016 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
2017 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
2018 Method->isInstanceMethod()))
2019 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002020 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002021
2022 return clang_getNullCursor();
2023 }
2024
2025 case Decl::ObjCCategory:
2026 if (ObjCCategoryImplDecl *Impl
2027 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002028 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002029 return clang_getNullCursor();
2030
2031 case Decl::ObjCProtocol:
2032 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
2033 return C;
2034 return clang_getNullCursor();
2035
2036 case Decl::ObjCInterface:
2037 // There are two notions of a "definition" for an Objective-C
2038 // class: the interface and its implementation. When we resolved a
2039 // reference to an Objective-C class, produce the @interface as
2040 // the definition; when we were provided with the interface,
2041 // produce the @implementation as the definition.
2042 if (WasReference) {
2043 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
2044 return C;
2045 } else if (ObjCImplementationDecl *Impl
2046 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002047 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002048 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002049
Douglas Gregorb6998662010-01-19 19:34:47 +00002050 case Decl::ObjCProperty:
2051 // FIXME: We don't really know where to find the
2052 // ObjCPropertyImplDecls that implement this property.
2053 return clang_getNullCursor();
2054
2055 case Decl::ObjCCompatibleAlias:
2056 if (ObjCInterfaceDecl *Class
2057 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
2058 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002059 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002060
Douglas Gregorb6998662010-01-19 19:34:47 +00002061 return clang_getNullCursor();
2062
2063 case Decl::ObjCForwardProtocol: {
2064 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
2065 if (Forward->protocol_size() == 1)
2066 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002067 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002068 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002069
2070 // FIXME: Cannot return multiple definitions.
2071 return clang_getNullCursor();
2072 }
2073
2074 case Decl::ObjCClass: {
2075 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
2076 if (Class->size() == 1) {
2077 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
2078 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002079 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00002080 return clang_getNullCursor();
2081 }
2082
2083 // FIXME: Cannot return multiple definitions.
2084 return clang_getNullCursor();
2085 }
2086
2087 case Decl::Friend:
2088 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002089 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002090 return clang_getNullCursor();
2091
2092 case Decl::FriendTemplate:
2093 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002094 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00002095 return clang_getNullCursor();
2096 }
2097
2098 return clang_getNullCursor();
2099}
2100
2101unsigned clang_isCursorDefinition(CXCursor C) {
2102 if (!clang_isDeclaration(C.kind))
2103 return 0;
2104
2105 return clang_getCursorDefinition(C) == C;
2106}
2107
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002108void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00002109 const char **startBuf,
2110 const char **endBuf,
2111 unsigned *startLine,
2112 unsigned *startColumn,
2113 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002114 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00002115 assert(getCursorDecl(C) && "CXCursor has null decl");
2116 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00002117 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
2118 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002119
Steve Naroff4ade6d62009-09-23 17:52:52 +00002120 SourceManager &SM = FD->getASTContext().getSourceManager();
2121 *startBuf = SM.getCharacterData(Body->getLBracLoc());
2122 *endBuf = SM.getCharacterData(Body->getRBracLoc());
2123 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
2124 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
2125 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
2126 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
2127}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002128
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002129void clang_enableStackTraces(void) {
2130 llvm::sys::PrintStackTraceOnErrorSignal();
2131}
2132
Ted Kremenekfb480492010-01-13 21:46:36 +00002133} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00002134
Ted Kremenekfb480492010-01-13 21:46:36 +00002135//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002136// Token-based Operations.
2137//===----------------------------------------------------------------------===//
2138
2139/* CXToken layout:
2140 * int_data[0]: a CXTokenKind
2141 * int_data[1]: starting token location
2142 * int_data[2]: token length
2143 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002144 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002145 * otherwise unused.
2146 */
2147extern "C" {
2148
2149CXTokenKind clang_getTokenKind(CXToken CXTok) {
2150 return static_cast<CXTokenKind>(CXTok.int_data[0]);
2151}
2152
2153CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
2154 switch (clang_getTokenKind(CXTok)) {
2155 case CXToken_Identifier:
2156 case CXToken_Keyword:
2157 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002158 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2159 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002160
2161 case CXToken_Literal: {
2162 // We have stashed the starting pointer in the ptr_data field. Use it.
2163 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002164 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002165 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002166
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002167 case CXToken_Punctuation:
2168 case CXToken_Comment:
2169 break;
2170 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002171
2172 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002173 // deconstructing the source location.
2174 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2175 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002176 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002177
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002178 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2179 std::pair<FileID, unsigned> LocInfo
2180 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +00002181 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002182 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002183 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
2184 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00002185 return createCXString("");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002186
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002187 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002188}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002189
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002190CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2191 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2192 if (!CXXUnit)
2193 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002194
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002195 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2196 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2197}
2198
2199CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2200 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002201 if (!CXXUnit)
2202 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002203
2204 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002205 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2206}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002207
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002208void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2209 CXToken **Tokens, unsigned *NumTokens) {
2210 if (Tokens)
2211 *Tokens = 0;
2212 if (NumTokens)
2213 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002214
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002215 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2216 if (!CXXUnit || !Tokens || !NumTokens)
2217 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002218
Douglas Gregorbdf60622010-03-05 21:16:25 +00002219 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2220
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002221 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002222 if (R.isInvalid())
2223 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002224
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002225 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2226 std::pair<FileID, unsigned> BeginLocInfo
2227 = SourceMgr.getDecomposedLoc(R.getBegin());
2228 std::pair<FileID, unsigned> EndLocInfo
2229 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002230
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002231 // Cannot tokenize across files.
2232 if (BeginLocInfo.first != EndLocInfo.first)
2233 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002234
2235 // Create a lexer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002236 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002237 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00002238 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
Douglas Gregor47a3fcd2010-03-16 20:26:15 +00002239 if (Invalid)
2240 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +00002241
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002242 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2243 CXXUnit->getASTContext().getLangOptions(),
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002244 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002245 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002246
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002247 // Lex tokens until we hit the end of the range.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002248 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002249 llvm::SmallVector<CXToken, 32> CXTokens;
2250 Token Tok;
2251 do {
2252 // Lex the next token
2253 Lex.LexFromRawLexer(Tok);
2254 if (Tok.is(tok::eof))
2255 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002256
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002257 // Initialize the CXToken.
2258 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002259
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002260 // - Common fields
2261 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2262 CXTok.int_data[2] = Tok.getLength();
2263 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002264
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002265 // - Kind-specific fields
2266 if (Tok.isLiteral()) {
2267 CXTok.int_data[0] = CXToken_Literal;
2268 CXTok.ptr_data = (void *)Tok.getLiteralData();
2269 } else if (Tok.is(tok::identifier)) {
Douglas Gregoraea67db2010-03-15 22:54:52 +00002270 // Lookup the identifier to determine whether we have a keyword.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002271 std::pair<FileID, unsigned> LocInfo
2272 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Douglas Gregorf715ca12010-03-16 00:06:06 +00002273 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002274 llvm::StringRef Buf
Douglas Gregorf715ca12010-03-16 00:06:06 +00002275 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
2276 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00002277 return;
2278
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00002279 const char *StartPos = Buf.data() + LocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002280 IdentifierInfo *II
2281 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2282 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2283 CXToken_Identifier
2284 : CXToken_Keyword;
2285 CXTok.ptr_data = II;
2286 } else if (Tok.is(tok::comment)) {
2287 CXTok.int_data[0] = CXToken_Comment;
2288 CXTok.ptr_data = 0;
2289 } else {
2290 CXTok.int_data[0] = CXToken_Punctuation;
2291 CXTok.ptr_data = 0;
2292 }
2293 CXTokens.push_back(CXTok);
2294 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002295
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002296 if (CXTokens.empty())
2297 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002298
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002299 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2300 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2301 *NumTokens = CXTokens.size();
2302}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002303
2304typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2305
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002306enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2307 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002308 CXClientData client_data) {
2309 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2310
2311 // We only annotate the locations of declarations, simple
2312 // references, and expressions which directly reference something.
2313 CXCursorKind Kind = clang_getCursorKind(cursor);
2314 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2315 // Okay: We can annotate the location of this declaration with the
2316 // declaration or reference
2317 } else if (clang_isExpression(cursor.kind)) {
2318 if (Kind != CXCursor_DeclRefExpr &&
2319 Kind != CXCursor_MemberRefExpr &&
2320 Kind != CXCursor_ObjCMessageExpr)
2321 return CXChildVisit_Recurse;
2322
2323 CXCursor Referenced = clang_getCursorReferenced(cursor);
2324 if (Referenced == cursor || Referenced == clang_getNullCursor())
2325 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002326
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002327 // Okay: we can annotate the location of this expression
Douglas Gregor0396f462010-03-19 05:22:59 +00002328 } else if (clang_isPreprocessing(cursor.kind)) {
2329 // We can always annotate a preprocessing directive/macro instantiation.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002330 } else {
2331 // Nothing to annotate
2332 return CXChildVisit_Recurse;
2333 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002334
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002335 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2336 (*Data)[Loc.int_data] = cursor;
2337 return CXChildVisit_Recurse;
2338}
2339
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002340void clang_annotateTokens(CXTranslationUnit TU,
2341 CXToken *Tokens, unsigned NumTokens,
2342 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002343 if (NumTokens == 0)
2344 return;
2345
2346 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002347 for (unsigned I = 0; I != NumTokens; ++I)
2348 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002349
2350 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2351 if (!CXXUnit || !Tokens)
2352 return;
2353
Douglas Gregorbdf60622010-03-05 21:16:25 +00002354 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2355
Douglas Gregor0396f462010-03-19 05:22:59 +00002356 // Determine the region of interest, which contains all of the tokens.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002357 SourceRange RegionOfInterest;
2358 RegionOfInterest.setBegin(
2359 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2360 SourceLocation End
Douglas Gregor09d9fa12010-04-05 16:10:30 +00002361 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0396f462010-03-19 05:22:59 +00002362 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002363 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor2507fa82010-03-19 00:18:31 +00002364
Douglas Gregor0396f462010-03-19 05:22:59 +00002365 // A mapping from the source locations found when re-lexing or traversing the
2366 // region of interest to the corresponding cursors.
2367 AnnotateTokensData Annotated;
2368
2369 // Relex the tokens within the source range to look for preprocessing
2370 // directives.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002371 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2372 std::pair<FileID, unsigned> BeginLocInfo
2373 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
2374 std::pair<FileID, unsigned> EndLocInfo
2375 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
2376
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002377 llvm::StringRef Buffer;
Douglas Gregor0396f462010-03-19 05:22:59 +00002378 bool Invalid = false;
2379 if (BeginLocInfo.first == EndLocInfo.first &&
2380 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
2381 !Invalid) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002382 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2383 CXXUnit->getASTContext().getLangOptions(),
Douglas Gregor4ae8f292010-03-18 17:52:52 +00002384 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
2385 Buffer.end());
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002386 Lex.SetCommentRetentionState(true);
2387
2388 // Lex tokens in raw mode until we hit the end of the range, to avoid
2389 // entering #includes or expanding macros.
Douglas Gregor48072312010-03-18 15:23:44 +00002390 while (true) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002391 Token Tok;
2392 Lex.LexFromRawLexer(Tok);
2393
2394 reprocess:
2395 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
2396 // We have found a preprocessing directive. Gobble it up so that we
2397 // don't see it while preprocessing these tokens later, but keep track of
2398 // all of the token locations inside this preprocessing directive so that
2399 // we can annotate them appropriately.
2400 //
2401 // FIXME: Some simple tests here could identify macro definitions and
2402 // #undefs, to provide specific cursor kinds for those.
2403 std::vector<SourceLocation> Locations;
2404 do {
2405 Locations.push_back(Tok.getLocation());
2406 Lex.LexFromRawLexer(Tok);
2407 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
2408
2409 using namespace cxcursor;
2410 CXCursor Cursor
2411 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
2412 Locations.back()),
2413 CXXUnit);
2414 for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
2415 Annotated[Locations[I].getRawEncoding()] = Cursor;
2416 }
2417
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002418 if (Tok.isAtStartOfLine())
2419 goto reprocess;
2420
2421 continue;
2422 }
2423
Douglas Gregor48072312010-03-18 15:23:44 +00002424 if (Tok.is(tok::eof))
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002425 break;
2426 }
Douglas Gregor4ae8f292010-03-18 17:52:52 +00002427 }
Douglas Gregor0396f462010-03-19 05:22:59 +00002428
2429 // Annotate all of the source locations in the region of interest that map to
2430 // a specific cursor.
2431 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
2432 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
2433 Decl::MaxPCHLevel, RegionOfInterest);
2434 AnnotateVis.VisitChildren(Parent);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002435
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002436 for (unsigned I = 0; I != NumTokens; ++I) {
2437 // Determine whether we saw a cursor at this token's location.
2438 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2439 if (Pos == Annotated.end())
2440 continue;
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002441
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002442 Cursors[I] = Pos->second;
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00002443 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002444}
2445
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002446void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002447 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002448 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002449}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002450
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002451} // end: extern "C"
2452
2453//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002454// Operations for querying linkage of a cursor.
2455//===----------------------------------------------------------------------===//
2456
2457extern "C" {
2458CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
Douglas Gregor0396f462010-03-19 05:22:59 +00002459 if (!clang_isDeclaration(cursor.kind))
2460 return CXLinkage_Invalid;
2461
Ted Kremenek16b42592010-03-03 06:36:57 +00002462 Decl *D = cxcursor::getCursorDecl(cursor);
2463 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2464 switch (ND->getLinkage()) {
2465 case NoLinkage: return CXLinkage_NoLinkage;
2466 case InternalLinkage: return CXLinkage_Internal;
2467 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2468 case ExternalLinkage: return CXLinkage_External;
2469 };
2470
2471 return CXLinkage_Invalid;
2472}
2473} // end: extern "C"
2474
2475//===----------------------------------------------------------------------===//
Ted Kremenek45e1dae2010-04-12 21:22:16 +00002476// Operations for querying language of a cursor.
2477//===----------------------------------------------------------------------===//
2478
2479static CXLanguageKind getDeclLanguage(const Decl *D) {
2480 switch (D->getKind()) {
2481 default:
2482 break;
2483 case Decl::ImplicitParam:
2484 case Decl::ObjCAtDefsField:
2485 case Decl::ObjCCategory:
2486 case Decl::ObjCCategoryImpl:
2487 case Decl::ObjCClass:
2488 case Decl::ObjCCompatibleAlias:
2489 case Decl::ObjCContainer:
2490 case Decl::ObjCForwardProtocol:
2491 case Decl::ObjCImplementation:
2492 case Decl::ObjCInterface:
2493 case Decl::ObjCIvar:
2494 case Decl::ObjCMethod:
2495 case Decl::ObjCProperty:
2496 case Decl::ObjCPropertyImpl:
2497 case Decl::ObjCProtocol:
2498 return CXLanguage_ObjC;
2499 case Decl::CXXConstructor:
2500 case Decl::CXXConversion:
2501 case Decl::CXXDestructor:
2502 case Decl::CXXMethod:
2503 case Decl::CXXRecord:
2504 case Decl::ClassTemplate:
2505 case Decl::ClassTemplatePartialSpecialization:
2506 case Decl::ClassTemplateSpecialization:
2507 case Decl::Friend:
2508 case Decl::FriendTemplate:
2509 case Decl::FunctionTemplate:
2510 case Decl::LinkageSpec:
2511 case Decl::Namespace:
2512 case Decl::NamespaceAlias:
2513 case Decl::NonTypeTemplateParm:
2514 case Decl::StaticAssert:
2515 case Decl::Template:
2516 case Decl::TemplateTemplateParm:
2517 case Decl::TemplateTypeParm:
2518 case Decl::UnresolvedUsingTypename:
2519 case Decl::UnresolvedUsingValue:
2520 case Decl::Using:
2521 case Decl::UsingDirective:
2522 case Decl::UsingShadow:
2523 return CXLanguage_CPlusPlus;
2524 }
2525
2526 return CXLanguage_C;
2527}
2528
2529extern "C" {
2530CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
2531 if (clang_isDeclaration(cursor.kind))
2532 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
2533
2534 return CXLanguage_Invalid;
2535}
2536} // end: extern "C"
2537
2538//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002539// CXString Operations.
2540//===----------------------------------------------------------------------===//
2541
2542extern "C" {
2543const char *clang_getCString(CXString string) {
2544 return string.Spelling;
2545}
2546
2547void clang_disposeString(CXString string) {
2548 if (string.MustFreeString && string.Spelling)
2549 free((void*)string.Spelling);
2550}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002551
Ted Kremenekfb480492010-01-13 21:46:36 +00002552} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002553
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002554namespace clang { namespace cxstring {
2555CXString createCXString(const char *String, bool DupString){
2556 CXString Str;
2557 if (DupString) {
2558 Str.Spelling = strdup(String);
2559 Str.MustFreeString = 1;
2560 } else {
2561 Str.Spelling = String;
2562 Str.MustFreeString = 0;
2563 }
2564 return Str;
2565}
2566
2567CXString createCXString(llvm::StringRef String, bool DupString) {
2568 CXString Result;
2569 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2570 char *Spelling = (char *)malloc(String.size() + 1);
2571 memmove(Spelling, String.data(), String.size());
2572 Spelling[String.size()] = 0;
2573 Result.Spelling = Spelling;
2574 Result.MustFreeString = 1;
2575 } else {
2576 Result.Spelling = String.data();
2577 Result.MustFreeString = 0;
2578 }
2579 return Result;
2580}
2581}}
2582
Ted Kremenek04bb7162010-01-22 22:44:15 +00002583//===----------------------------------------------------------------------===//
2584// Misc. utility functions.
2585//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002586
Ted Kremenek04bb7162010-01-22 22:44:15 +00002587extern "C" {
2588
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002589CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002590 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002591}
2592
2593} // end: extern "C"