blob: f07b94fc51d41ceb97a0cbf853a85e604e4848bf [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenekab188932010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekd2fa5662009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "CIndexer.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000017#include "CXSourceLocation.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000018#include "CIndexDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000019
Ted Kremenek04bb7162010-01-22 22:44:15 +000020#include "clang/Basic/Version.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000021
Steve Naroff50398192009-08-28 15:28:48 +000022#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000023#include "clang/AST/StmtVisitor.h"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000025#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000026#include "clang/Lex/Lexer.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000027#include "clang/Lex/Preprocessor.h"
Douglas Gregor02465752009-10-16 21:24:31 +000028#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Douglas Gregor0a812cf2010-02-18 23:07:20 +000030#include "llvm/System/Signals.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000031
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000032// Needed to define L_TMPNAM on some systems.
33#include <cstdio>
34
Steve Naroff50398192009-08-28 15:28:48 +000035using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000036using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000037using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000038using namespace idx;
39
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000040//===----------------------------------------------------------------------===//
41// Crash Reporting.
42//===----------------------------------------------------------------------===//
43
44#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000045#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000046#include "clang/Analysis/Support/SaveAndRestore.h"
47// Integrate with crash reporter.
48extern "C" const char *__crashreporter_info__;
Ted Kremenek6b569992010-02-17 21:12:23 +000049#define NUM_CRASH_STRINGS 32
Ted Kremenek29b72842010-01-07 22:49:05 +000050static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000051static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000052static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
53static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
54
55static unsigned SetCrashTracerInfo(const char *str,
56 llvm::SmallString<1024> &AggStr) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000057
Ted Kremenek254ba7c2010-01-07 23:13:53 +000058 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000059 while (crashtracer_strings[slot]) {
60 if (++slot == NUM_CRASH_STRINGS)
61 slot = 0;
62 }
63 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000064 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000065
66 // We need to create an aggregate string because multiple threads
67 // may be in this method at one time. The crash reporter string
68 // will attempt to overapproximate the set of in-flight invocations
69 // of this function. Race conditions can still cause this goal
70 // to not be achieved.
71 {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000072 llvm::raw_svector_ostream Out(AggStr);
Ted Kremenek29b72842010-01-07 22:49:05 +000073 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
74 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
75 }
76 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
77 return slot;
78}
79
80static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000081 unsigned max_slot = 0;
82 unsigned max_value = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +000083
Ted Kremenek254ba7c2010-01-07 23:13:53 +000084 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
85
86 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
87 if (agg_crashtracer_strings[i] &&
88 crashtracer_counter_id[i] > max_value) {
89 max_slot = i;
90 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000091 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000092
93 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000094}
95
96namespace {
97class ArgsCrashTracerInfo {
98 llvm::SmallString<1024> CrashString;
99 llvm::SmallString<1024> AggregateString;
100 unsigned crashtracerSlot;
101public:
102 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
103 : crashtracerSlot(0)
104 {
105 {
106 llvm::raw_svector_ostream Out(CrashString);
Ted Kremenek0baa9522010-03-05 22:43:25 +0000107 Out << "ClangCIndex [" << getClangFullVersion() << "]"
108 << "[createTranslationUnitFromSourceFile]: clang";
Ted Kremenek29b72842010-01-07 22:49:05 +0000109 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
110 E=Args.end(); I!=E; ++I)
111 Out << ' ' << *I;
112 }
113 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
114 AggregateString);
115 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000116
Ted Kremenek29b72842010-01-07 22:49:05 +0000117 ~ArgsCrashTracerInfo() {
118 ResetCrashTracerInfo(crashtracerSlot);
119 }
120};
121}
122#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000123
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000124/// \brief The result of comparing two source ranges.
125enum RangeComparisonResult {
126 /// \brief Either the ranges overlap or one of the ranges is invalid.
127 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000128
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000129 /// \brief The first range ends before the second range starts.
130 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000131
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000132 /// \brief The first range starts after the second range ends.
133 RangeAfter
134};
135
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000136/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000137/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000138static RangeComparisonResult RangeCompare(SourceManager &SM,
139 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000140 SourceRange R2) {
141 assert(R1.isValid() && "First range is invalid?");
142 assert(R2.isValid() && "Second range is invalid?");
Daniel Dunbard52864b2010-02-14 10:02:57 +0000143 if (R1.getEnd() == R2.getBegin() ||
144 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000145 return RangeBefore;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000146 if (R2.getEnd() == R1.getBegin() ||
147 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000148 return RangeAfter;
149 return RangeOverlap;
150}
151
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000152/// \brief Translate a Clang source range into a CIndex source range.
153///
154/// Clang internally represents ranges where the end location points to the
155/// start of the token at the end. However, for external clients it is more
156/// useful to have a CXSourceRange be a proper half-open interval. This routine
157/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000158CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000159 const LangOptions &LangOpts,
160 SourceRange R) {
161 // FIXME: This is largely copy-paste from
162 // TextDiagnosticPrinter::HighlightRange. When it is clear that this is what
163 // we want the two routines should be refactored.
164
165 // We want the last character in this location, so we will adjust the
166 // instantiation location accordingly.
167
168 // If the location is from a macro instantiation, get the end of the
169 // instantiation range.
170 SourceLocation EndLoc = R.getEnd();
171 SourceLocation InstLoc = SM.getInstantiationLoc(EndLoc);
172 if (EndLoc.isMacroID())
173 InstLoc = SM.getInstantiationRange(EndLoc).second;
174
175 // Measure the length token we're pointing at, so we can adjust the physical
176 // location in the file to point at the last character.
177 //
178 // FIXME: This won't cope with trigraphs or escaped newlines well. For that,
179 // we actually need a preprocessor, which isn't currently available
180 // here. Eventually, we'll switch the pointer data of
181 // CXSourceLocation/CXSourceRange to a translation unit (CXXUnit), so that the
182 // preprocessor will be available here. At that point, we can use
183 // Preprocessor::getLocForEndOfToken().
184 if (InstLoc.isValid()) {
185 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000186 EndLoc = EndLoc.getFileLocWithOffset(Length);
187 }
188
189 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
190 R.getBegin().getRawEncoding(),
191 EndLoc.getRawEncoding() };
192 return Result;
193}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000194
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000195//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000196// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000197//===----------------------------------------------------------------------===//
198
Steve Naroff89922f82009-08-31 00:59:03 +0000199namespace {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000200
Douglas Gregorb1373d02010-01-20 20:59:29 +0000201// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000202class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregora59e3902010-01-21 23:27:09 +0000203 public TypeLocVisitor<CursorVisitor, bool>,
204 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000205{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000206 /// \brief The translation unit we are traversing.
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000207 ASTUnit *TU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000208
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000209 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000210 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000211
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000212 /// \brief The declaration that serves at the parent of any statement or
213 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000214 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000215
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000216 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000217 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000218
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000219 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000220 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000221
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000222 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
223 // to the visitor. Declarations with a PCH level greater than this value will
224 // be suppressed.
225 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000226
227 /// \brief When valid, a source range to which the cursor should restrict
228 /// its search.
229 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000230
Douglas Gregorb1373d02010-01-20 20:59:29 +0000231 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000232 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregora59e3902010-01-21 23:27:09 +0000233 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000234
235 /// \brief Determine whether this particular source range comes before, comes
236 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000237 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000238 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000239 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
240
Steve Naroff89922f82009-08-31 00:59:03 +0000241public:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000242 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
243 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000244 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000245 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000246 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000247 {
248 Parent.kind = CXCursor_NoDeclFound;
249 Parent.data[0] = 0;
250 Parent.data[1] = 0;
251 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000252 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000253 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000254
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000255 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000256 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000257
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000258 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000259 bool VisitAttributes(Decl *D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000260 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000261 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
262 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000263 bool VisitTagDecl(TagDecl *D);
264 bool VisitEnumConstantDecl(EnumConstantDecl *D);
265 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
266 bool VisitFunctionDecl(FunctionDecl *ND);
267 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000268 bool VisitVarDecl(VarDecl *);
Ted Kremenek79758f62010-02-18 22:36:18 +0000269 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
270 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
271 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
272 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
273 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
274 bool VisitObjCImplDecl(ObjCImplDecl *D);
275 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
276 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
277 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
278 // etc.
279 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
280 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
281 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000282
283 // Type visitors
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000284 // FIXME: QualifiedTypeLoc doesn't provide any location information
285 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000286 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000287 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
288 bool VisitTagTypeLoc(TagTypeLoc TL);
289 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
290 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
291 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
292 bool VisitPointerTypeLoc(PointerTypeLoc TL);
293 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
294 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
295 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
296 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
297 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
298 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000299 // FIXME: Implement for TemplateSpecializationTypeLoc
300 // FIXME: Implement visitors here when the unimplemented TypeLocs get
301 // implemented
302 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
303 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000304
Douglas Gregora59e3902010-01-21 23:27:09 +0000305 // Statement visitors
306 bool VisitStmt(Stmt *S);
307 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000308 // FIXME: LabelStmt label?
309 bool VisitIfStmt(IfStmt *S);
310 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000311 bool VisitWhileStmt(WhileStmt *S);
312 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000313
Douglas Gregor336fd812010-01-23 00:40:08 +0000314 // Expression visitors
315 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
316 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
317 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000318};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000319
Ted Kremenekab188932010-01-05 19:32:54 +0000320} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000321
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000322RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000323 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
324}
325
Douglas Gregorb1373d02010-01-20 20:59:29 +0000326/// \brief Visit the given cursor and, if requested by the visitor,
327/// its children.
328///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000329/// \param Cursor the cursor to visit.
330///
331/// \param CheckRegionOfInterest if true, then the caller already checked that
332/// this cursor is within the region of interest.
333///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000334/// \returns true if the visitation should be aborted, false if it
335/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000336bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000337 if (clang_isInvalid(Cursor.kind))
338 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000339
Douglas Gregorb1373d02010-01-20 20:59:29 +0000340 if (clang_isDeclaration(Cursor.kind)) {
341 Decl *D = getCursorDecl(Cursor);
342 assert(D && "Invalid declaration cursor");
343 if (D->getPCHLevel() > MaxPCHLevel)
344 return false;
345
346 if (D->isImplicit())
347 return false;
348 }
349
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000350 // If we have a range of interest, and this cursor doesn't intersect with it,
351 // we're done.
352 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000353 SourceRange Range =
354 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
355 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000356 return false;
357 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000358
Douglas Gregorb1373d02010-01-20 20:59:29 +0000359 switch (Visitor(Cursor, Parent, ClientData)) {
360 case CXChildVisit_Break:
361 return true;
362
363 case CXChildVisit_Continue:
364 return false;
365
366 case CXChildVisit_Recurse:
367 return VisitChildren(Cursor);
368 }
369
Douglas Gregorfd643772010-01-25 16:45:46 +0000370 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000371}
372
373/// \brief Visit the children of the given cursor.
374///
375/// \returns true if the visitation should be aborted, false if it
376/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000377bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000378 if (clang_isReference(Cursor.kind)) {
379 // By definition, references have no children.
380 return false;
381 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000382
383 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000384 // done.
385 class SetParentRAII {
386 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000387 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000388 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000389
Douglas Gregorb1373d02010-01-20 20:59:29 +0000390 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000391 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000392 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000393 {
394 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000395 if (clang_isDeclaration(Parent.kind))
396 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000397 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000398
Douglas Gregorb1373d02010-01-20 20:59:29 +0000399 ~SetParentRAII() {
400 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000401 if (clang_isDeclaration(Parent.kind))
402 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000403 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000404 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000405
Douglas Gregorb1373d02010-01-20 20:59:29 +0000406 if (clang_isDeclaration(Cursor.kind)) {
407 Decl *D = getCursorDecl(Cursor);
408 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000409 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000410 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000411
Douglas Gregora59e3902010-01-21 23:27:09 +0000412 if (clang_isStatement(Cursor.kind))
413 return Visit(getCursorStmt(Cursor));
414 if (clang_isExpression(Cursor.kind))
415 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000416
Douglas Gregorb1373d02010-01-20 20:59:29 +0000417 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000418 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000419 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
420 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000421 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
422 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
423 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000424 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000425 return true;
426 }
427 } else {
428 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000429 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000430 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000431
Douglas Gregor7b691f332010-01-20 21:13:59 +0000432 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000433 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000434
Douglas Gregorb1373d02010-01-20 20:59:29 +0000435 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000436 return false;
437}
438
Douglas Gregorb1373d02010-01-20 20:59:29 +0000439bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000440 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000441 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek09dfa372010-02-18 05:46:33 +0000442
Daniel Dunbard52864b2010-02-14 10:02:57 +0000443 CXCursor Cursor = MakeCXCursor(*I, TU);
444
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000445 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000446 SourceRange Range =
447 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
448 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000449 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000450
451 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000452 case RangeBefore:
453 // This declaration comes before the region of interest; skip it.
454 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000455
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000456 case RangeAfter:
457 // This declaration comes after the region of interest; we're done.
458 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000459
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000460 case RangeOverlap:
461 // This declaration overlaps the region of interest; visit it.
462 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000463 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000464 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000465
Daniel Dunbard52864b2010-02-14 10:02:57 +0000466 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000467 return true;
468 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000469
Douglas Gregorb1373d02010-01-20 20:59:29 +0000470 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000471}
472
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000473bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
474 llvm_unreachable("Translation units are visited directly by Visit()");
475 return false;
476}
477
478bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
479 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
480 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000481
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000482 return false;
483}
484
485bool CursorVisitor::VisitTagDecl(TagDecl *D) {
486 return VisitDeclContext(D);
487}
488
489bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
490 if (Expr *Init = D->getInitExpr())
491 return Visit(MakeCXCursor(Init, StmtParent, TU));
492 return false;
493}
494
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000495bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
496 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
497 if (Visit(TSInfo->getTypeLoc()))
498 return true;
499
500 return false;
501}
502
Douglas Gregorb1373d02010-01-20 20:59:29 +0000503bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000504 if (VisitDeclaratorDecl(ND))
505 return true;
506
Douglas Gregora59e3902010-01-21 23:27:09 +0000507 if (ND->isThisDeclarationADefinition() &&
508 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
509 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000510
Douglas Gregorb1373d02010-01-20 20:59:29 +0000511 return false;
512}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000513
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000514bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
515 if (VisitDeclaratorDecl(D))
516 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000517
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000518 if (Expr *BitWidth = D->getBitWidth())
519 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000520
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000521 return false;
522}
523
524bool CursorVisitor::VisitVarDecl(VarDecl *D) {
525 if (VisitDeclaratorDecl(D))
526 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000527
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000528 if (Expr *Init = D->getInit())
529 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000530
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000531 return false;
532}
533
534bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
535 // FIXME: We really need a TypeLoc covering Objective-C method declarations.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000536 // At the moment, we don't have information about locations in the return
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000537 // type.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000538 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000539 PEnd = ND->param_end();
540 P != PEnd; ++P) {
541 if (Visit(MakeCXCursor(*P, TU)))
542 return true;
543 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000544
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000545 if (ND->isThisDeclarationADefinition() &&
546 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
547 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000548
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000549 return false;
550}
551
Douglas Gregora59e3902010-01-21 23:27:09 +0000552bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
553 return VisitDeclContext(D);
554}
555
Douglas Gregorb1373d02010-01-20 20:59:29 +0000556bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000557 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
558 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000559 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000560
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000561 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
562 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
563 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000564 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000565 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000566
Douglas Gregora59e3902010-01-21 23:27:09 +0000567 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000568}
569
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000570bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
571 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
572 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
573 E = PID->protocol_end(); I != E; ++I, ++PL)
574 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
575 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000576
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000577 return VisitObjCContainerDecl(PID);
578}
579
Douglas Gregorb1373d02010-01-20 20:59:29 +0000580bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000581 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000582 if (D->getSuperClass() &&
583 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000584 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000585 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000586 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000587
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000588 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
589 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
590 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000591 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000592 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000593
Douglas Gregora59e3902010-01-21 23:27:09 +0000594 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000595}
596
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000597bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
598 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000599}
600
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000601bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000602 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000603 D->getLocation(), TU)))
604 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000605
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000606 return VisitObjCImplDecl(D);
607}
608
609bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
610#if 0
611 // Issue callbacks for super class.
612 // FIXME: No source location information!
613 if (D->getSuperClass() &&
614 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000615 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000616 TU)))
617 return true;
618#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000619
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000620 return VisitObjCImplDecl(D);
621}
622
623bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
624 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
625 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
626 E = D->protocol_end();
627 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
631 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000632}
633
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000634bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
635 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
636 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
637 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000638
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000639 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000640}
641
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000642bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
643 ASTContext &Context = TU->getASTContext();
644
645 // Some builtin types (such as Objective-C's "id", "sel", and
646 // "Class") have associated declarations. Create cursors for those.
647 QualType VisitType;
648 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000649 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000650 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000651 case BuiltinType::Char_U:
652 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000653 case BuiltinType::Char16:
654 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000655 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000656 case BuiltinType::UInt:
657 case BuiltinType::ULong:
658 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000659 case BuiltinType::UInt128:
660 case BuiltinType::Char_S:
661 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000662 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000663 case BuiltinType::Short:
664 case BuiltinType::Int:
665 case BuiltinType::Long:
666 case BuiltinType::LongLong:
667 case BuiltinType::Int128:
668 case BuiltinType::Float:
669 case BuiltinType::Double:
670 case BuiltinType::LongDouble:
671 case BuiltinType::NullPtr:
672 case BuiltinType::Overload:
673 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000674 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000675
676 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000677 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000678
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000679 case BuiltinType::ObjCId:
680 VisitType = Context.getObjCIdType();
681 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000682
683 case BuiltinType::ObjCClass:
684 VisitType = Context.getObjCClassType();
685 break;
686
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000687 case BuiltinType::ObjCSel:
688 VisitType = Context.getObjCSelType();
689 break;
690 }
691
692 if (!VisitType.isNull()) {
693 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000694 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000695 TU));
696 }
697
698 return false;
699}
700
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000701bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
702 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
703}
704
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000705bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
706 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
707}
708
709bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
710 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
711}
712
713bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
714 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
715 return true;
716
717 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
718 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
719 TU)))
720 return true;
721 }
722
723 return false;
724}
725
726bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
727 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
728 return true;
729
730 if (TL.hasProtocolsAsWritten()) {
731 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000732 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000733 TL.getProtocolLoc(I),
734 TU)))
735 return true;
736 }
737 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000738
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000739 return false;
740}
741
742bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
743 return Visit(TL.getPointeeLoc());
744}
745
746bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
747 return Visit(TL.getPointeeLoc());
748}
749
750bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
751 return Visit(TL.getPointeeLoc());
752}
753
754bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000755 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000756}
757
758bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000759 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000760}
761
762bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
763 if (Visit(TL.getResultLoc()))
764 return true;
765
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000766 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
767 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
768 return true;
769
770 return false;
771}
772
773bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
774 if (Visit(TL.getElementLoc()))
775 return true;
776
777 if (Expr *Size = TL.getSizeExpr())
778 return Visit(MakeCXCursor(Size, StmtParent, TU));
779
780 return false;
781}
782
Douglas Gregor2332c112010-01-21 20:48:56 +0000783bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
784 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
785}
786
787bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
788 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
789 return Visit(TSInfo->getTypeLoc());
790
791 return false;
792}
793
Douglas Gregora59e3902010-01-21 23:27:09 +0000794bool CursorVisitor::VisitStmt(Stmt *S) {
795 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
796 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000797 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000798 return true;
799 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000800
Douglas Gregora59e3902010-01-21 23:27:09 +0000801 return false;
802}
803
804bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
805 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
806 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000807 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000808 return true;
809 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000810
Douglas Gregora59e3902010-01-21 23:27:09 +0000811 return false;
812}
813
Douglas Gregorf5bab412010-01-22 01:00:11 +0000814bool CursorVisitor::VisitIfStmt(IfStmt *S) {
815 if (VarDecl *Var = S->getConditionVariable()) {
816 if (Visit(MakeCXCursor(Var, TU)))
817 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000818 }
819
Douglas Gregor263b47b2010-01-25 16:12:32 +0000820 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
821 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000822 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
823 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000824 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
825 return true;
826
827 return false;
828}
829
830bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
831 if (VarDecl *Var = S->getConditionVariable()) {
832 if (Visit(MakeCXCursor(Var, TU)))
833 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000834 }
835
Douglas Gregor263b47b2010-01-25 16:12:32 +0000836 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
837 return true;
838 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
839 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000840
Douglas Gregor263b47b2010-01-25 16:12:32 +0000841 return false;
842}
843
844bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
845 if (VarDecl *Var = S->getConditionVariable()) {
846 if (Visit(MakeCXCursor(Var, TU)))
847 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000848 }
849
Douglas Gregor263b47b2010-01-25 16:12:32 +0000850 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
851 return true;
852 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000853 return true;
854
Douglas Gregor263b47b2010-01-25 16:12:32 +0000855 return false;
856}
857
858bool CursorVisitor::VisitForStmt(ForStmt *S) {
859 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
860 return true;
861 if (VarDecl *Var = S->getConditionVariable()) {
862 if (Visit(MakeCXCursor(Var, TU)))
863 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000864 }
865
Douglas Gregor263b47b2010-01-25 16:12:32 +0000866 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
867 return true;
868 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
869 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000870 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
871 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000872
Douglas Gregorf5bab412010-01-22 01:00:11 +0000873 return false;
874}
875
Douglas Gregor336fd812010-01-23 00:40:08 +0000876bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
877 if (E->isArgumentType()) {
878 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
879 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000880
Douglas Gregor336fd812010-01-23 00:40:08 +0000881 return false;
882 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000883
Douglas Gregor336fd812010-01-23 00:40:08 +0000884 return VisitExpr(E);
885}
886
887bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
888 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
889 if (Visit(TSInfo->getTypeLoc()))
890 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000891
Douglas Gregor336fd812010-01-23 00:40:08 +0000892 return VisitCastExpr(E);
893}
894
895bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
896 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
897 if (Visit(TSInfo->getTypeLoc()))
898 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000899
Douglas Gregor336fd812010-01-23 00:40:08 +0000900 return VisitExpr(E);
901}
902
Ted Kremenek09dfa372010-02-18 05:46:33 +0000903bool CursorVisitor::VisitAttributes(Decl *D) {
904 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
905 if (Visit(MakeCXCursor(A, D, TU)))
906 return true;
907
908 return false;
909}
910
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000911extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000912CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
913 int displayDiagnostics) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000914 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000915 if (excludeDeclarationsFromPCH)
916 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000917 if (displayDiagnostics)
918 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000919 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000920}
921
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000922void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000923 if (CIdx)
924 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000925}
926
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000927void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000928 if (CIdx) {
929 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
930 CXXIdx->setUseExternalASTGeneration(value);
931 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000932}
933
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000934CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +0000935 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000936 if (!CIdx)
937 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000938
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000939 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000940
Douglas Gregor5352ac02010-01-28 00:27:43 +0000941 // Configure the diagnostics.
942 DiagnosticOptions DiagOpts;
943 llvm::OwningPtr<Diagnostic> Diags;
944 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000945 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000946 CXXIdx->getOnlyLocalDecls(),
947 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000948}
949
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000950CXTranslationUnit
951clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
952 const char *source_filename,
953 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000954 const char **command_line_args,
955 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000956 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000957 if (!CIdx)
958 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000959
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000960 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
961
Douglas Gregor5352ac02010-01-28 00:27:43 +0000962 // Configure the diagnostics.
963 DiagnosticOptions DiagOpts;
964 llvm::OwningPtr<Diagnostic> Diags;
965 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000966
Douglas Gregor4db64a42010-01-23 00:14:00 +0000967 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
968 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000969 const llvm::MemoryBuffer *Buffer
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000970 = llvm::MemoryBuffer::getMemBufferCopy(unsaved_files[I].Contents,
Douglas Gregor313e26c2010-02-18 23:35:40 +0000971 unsaved_files[I].Contents + unsaved_files[I].Length,
972 unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000973 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
974 Buffer));
975 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000976
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000977 if (!CXXIdx->getUseExternalASTGeneration()) {
978 llvm::SmallVector<const char *, 16> Args;
979
980 // The 'source_filename' argument is optional. If the caller does not
981 // specify it then it is assumed that the source file is specified
982 // in the actual argument list.
983 if (source_filename)
984 Args.push_back(source_filename);
985 Args.insert(Args.end(), command_line_args,
986 command_line_args + num_command_line_args);
987
Douglas Gregor5352ac02010-01-28 00:27:43 +0000988 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000989
Ted Kremenek29b72842010-01-07 22:49:05 +0000990#ifdef USE_CRASHTRACER
991 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000992#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000993
Daniel Dunbar94220972009-12-05 02:17:18 +0000994 llvm::OwningPtr<ASTUnit> Unit(
995 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000996 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000997 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000998 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +0000999 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001000 RemappedFiles.size(),
1001 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001002
Daniel Dunbar94220972009-12-05 02:17:18 +00001003 // FIXME: Until we have broader testing, just drop the entire AST if we
1004 // encountered an error.
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001005 if (NumErrors != Diags->getNumErrors()) {
1006 if (CXXIdx->getDisplayDiagnostics()) {
1007 for (ASTUnit::diag_iterator D = Unit->diag_begin(),
1008 DEnd = Unit->diag_end();
1009 D != DEnd; ++D) {
1010 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001011 CXString Msg = clang_formatDiagnostic(&Diag,
1012 clang_defaultDiagnosticDisplayOptions());
1013 fprintf(stderr, "%s\n", clang_getCString(Msg));
1014 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001015 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001016#ifdef LLVM_ON_WIN32
1017 // On Windows, force a flush, since there may be multiple copies of
1018 // stderr and stdout in the file system, all with different buffers
1019 // but writing to the same device.
1020 fflush(stderr);
1021#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001022 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001023 return 0;
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001024 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001025
1026 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001027 }
1028
Ted Kremenek139ba862009-10-22 00:03:57 +00001029 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001030 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001031
Ted Kremenek139ba862009-10-22 00:03:57 +00001032 // First add the complete path to the 'clang' executable.
1033 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001034 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001035
Ted Kremenek139ba862009-10-22 00:03:57 +00001036 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001037 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001038
Ted Kremenek139ba862009-10-22 00:03:57 +00001039 // The 'source_filename' argument is optional. If the caller does not
1040 // specify it then it is assumed that the source file is specified
1041 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001042 if (source_filename)
1043 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001044
Steve Naroff37b5ac22009-10-15 20:50:09 +00001045 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001046 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001047 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001048 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001049
Douglas Gregor4db64a42010-01-23 00:14:00 +00001050 // Remap any unsaved files to temporary files.
1051 std::vector<llvm::sys::Path> TemporaryFiles;
1052 std::vector<std::string> RemapArgs;
1053 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1054 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001055
Douglas Gregor4db64a42010-01-23 00:14:00 +00001056 // The pointers into the elements of RemapArgs are stable because we
1057 // won't be adding anything to RemapArgs after this point.
1058 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1059 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001060
Ted Kremenek139ba862009-10-22 00:03:57 +00001061 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1062 for (int i = 0; i < num_command_line_args; ++i)
1063 if (const char *arg = command_line_args[i]) {
1064 if (strcmp(arg, "-o") == 0) {
1065 ++i; // Also skip the matching argument.
1066 continue;
1067 }
1068 if (strcmp(arg, "-emit-ast") == 0 ||
1069 strcmp(arg, "-c") == 0 ||
1070 strcmp(arg, "-fsyntax-only") == 0) {
1071 continue;
1072 }
1073
1074 // Keep the argument.
1075 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001076 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001077
Douglas Gregord93256e2010-01-28 06:00:51 +00001078 // Generate a temporary name for the diagnostics file.
1079 char tmpFileResults[L_tmpnam];
1080 char *tmpResultsFileName = tmpnam(tmpFileResults);
1081 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1082 TemporaryFiles.push_back(DiagnosticsFile);
1083 argv.push_back("-fdiagnostics-binary");
1084
Ted Kremenek139ba862009-10-22 00:03:57 +00001085 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001086 argv.push_back(NULL);
1087
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001088 // Invoke 'clang'.
1089 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1090 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001091 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001092 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1093 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001094 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001095 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001096 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001097
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001098 if (!ErrMsg.empty()) {
1099 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001100 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001101 I != E; ++I) {
1102 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001103 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001104 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001105 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001106
Daniel Dunbar32141c82010-02-23 20:23:45 +00001107 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001108 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001109
Douglas Gregor5352ac02010-01-28 00:27:43 +00001110 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001111 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001112 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001113 RemappedFiles.size(),
1114 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001115 if (ATU) {
1116 LoadSerializedDiagnostics(DiagnosticsFile,
1117 num_unsaved_files, unsaved_files,
1118 ATU->getFileManager(),
1119 ATU->getSourceManager(),
1120 ATU->getDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001121 } else if (CXXIdx->getDisplayDiagnostics()) {
1122 // We failed to load the ASTUnit, but we can still deserialize the
1123 // diagnostics and emit them.
1124 FileManager FileMgr;
1125 SourceManager SourceMgr;
1126 // FIXME: Faked LangOpts!
1127 LangOptions LangOpts;
1128 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1129 LoadSerializedDiagnostics(DiagnosticsFile,
1130 num_unsaved_files, unsaved_files,
1131 FileMgr, SourceMgr, Diags);
1132 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1133 DEnd = Diags.end();
1134 D != DEnd; ++D) {
1135 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001136 CXString Msg = clang_formatDiagnostic(&Diag,
1137 clang_defaultDiagnosticDisplayOptions());
1138 fprintf(stderr, "%s\n", clang_getCString(Msg));
1139 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001140 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001141
1142#ifdef LLVM_ON_WIN32
1143 // On Windows, force a flush, since there may be multiple copies of
1144 // stderr and stdout in the file system, all with different buffers
1145 // but writing to the same device.
1146 fflush(stderr);
1147#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001148 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001149
Douglas Gregor313e26c2010-02-18 23:35:40 +00001150 if (ATU) {
1151 // Make the translation unit responsible for destroying all temporary files.
1152 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1153 ATU->addTemporaryFile(TemporaryFiles[i]);
1154 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1155 } else {
1156 // Destroy all of the temporary files now; they can't be referenced any
1157 // longer.
1158 llvm::sys::Path(astTmpFile).eraseFromDisk();
1159 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1160 TemporaryFiles[i].eraseFromDisk();
1161 }
1162
Steve Naroffe19944c2009-10-15 22:23:48 +00001163 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001164}
1165
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001166void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001167 if (CTUnit)
1168 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001169}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001170
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001171CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001172 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001173 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001174
Steve Naroff77accc12009-09-03 18:19:54 +00001175 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001176 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001177}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001178
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001179CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001180 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001181 return Result;
1182}
1183
Ted Kremenekfb480492010-01-13 21:46:36 +00001184} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001185
Ted Kremenekfb480492010-01-13 21:46:36 +00001186//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001187// CXSourceLocation and CXSourceRange Operations.
1188//===----------------------------------------------------------------------===//
1189
Douglas Gregorb9790342010-01-22 21:44:22 +00001190extern "C" {
1191CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001192 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001193 return Result;
1194}
1195
1196unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001197 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1198 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1199 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001200}
1201
1202CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1203 CXFile file,
1204 unsigned line,
1205 unsigned column) {
1206 if (!tu)
1207 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001208
Douglas Gregorb9790342010-01-22 21:44:22 +00001209 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1210 SourceLocation SLoc
1211 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001212 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001213 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001214
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001215 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001216}
1217
Douglas Gregor5352ac02010-01-28 00:27:43 +00001218CXSourceRange clang_getNullRange() {
1219 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1220 return Result;
1221}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001222
Douglas Gregor5352ac02010-01-28 00:27:43 +00001223CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1224 if (begin.ptr_data[0] != end.ptr_data[0] ||
1225 begin.ptr_data[1] != end.ptr_data[1])
1226 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001227
1228 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001229 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001230 return Result;
1231}
1232
Douglas Gregor46766dc2010-01-26 19:19:08 +00001233void clang_getInstantiationLocation(CXSourceLocation location,
1234 CXFile *file,
1235 unsigned *line,
1236 unsigned *column,
1237 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001238 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1239
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001240 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001241 if (file)
1242 *file = 0;
1243 if (line)
1244 *line = 0;
1245 if (column)
1246 *column = 0;
1247 if (offset)
1248 *offset = 0;
1249 return;
1250 }
1251
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001252 const SourceManager &SM =
1253 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001254 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001255
1256 if (file)
1257 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1258 if (line)
1259 *line = SM.getInstantiationLineNumber(InstLoc);
1260 if (column)
1261 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001262 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001263 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001264}
1265
Douglas Gregor1db19de2010-01-19 21:36:55 +00001266CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001267 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001268 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001269 return Result;
1270}
1271
1272CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001273 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001274 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001275 return Result;
1276}
1277
Douglas Gregorb9790342010-01-22 21:44:22 +00001278} // end: extern "C"
1279
Douglas Gregor1db19de2010-01-19 21:36:55 +00001280//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001281// CXFile Operations.
1282//===----------------------------------------------------------------------===//
1283
1284extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001285CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001286 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001287 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001288
Steve Naroff88145032009-10-27 14:35:18 +00001289 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001290 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001291}
1292
1293time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001294 if (!SFile)
1295 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001296
Steve Naroff88145032009-10-27 14:35:18 +00001297 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1298 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001299}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001300
Douglas Gregorb9790342010-01-22 21:44:22 +00001301CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1302 if (!tu)
1303 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001304
Douglas Gregorb9790342010-01-22 21:44:22 +00001305 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001306
Douglas Gregorb9790342010-01-22 21:44:22 +00001307 FileManager &FMgr = CXXUnit->getFileManager();
1308 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1309 return const_cast<FileEntry *>(File);
1310}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001311
Ted Kremenekfb480492010-01-13 21:46:36 +00001312} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001313
Ted Kremenekfb480492010-01-13 21:46:36 +00001314//===----------------------------------------------------------------------===//
1315// CXCursor Operations.
1316//===----------------------------------------------------------------------===//
1317
Ted Kremenekfb480492010-01-13 21:46:36 +00001318static Decl *getDeclFromExpr(Stmt *E) {
1319 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1320 return RefExpr->getDecl();
1321 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1322 return ME->getMemberDecl();
1323 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1324 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001325
Ted Kremenekfb480492010-01-13 21:46:36 +00001326 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1327 return getDeclFromExpr(CE->getCallee());
1328 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1329 return getDeclFromExpr(CE->getSubExpr());
1330 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1331 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001332
Ted Kremenekfb480492010-01-13 21:46:36 +00001333 return 0;
1334}
1335
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001336static SourceLocation getLocationFromExpr(Expr *E) {
1337 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1338 return /*FIXME:*/Msg->getLeftLoc();
1339 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1340 return DRE->getLocation();
1341 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1342 return Member->getMemberLoc();
1343 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1344 return Ivar->getLocation();
1345 return E->getLocStart();
1346}
1347
Ted Kremenekfb480492010-01-13 21:46:36 +00001348extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001349
1350unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001351 CXCursorVisitor visitor,
1352 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001353 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001354
1355 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001356
Douglas Gregorb1373d02010-01-20 20:59:29 +00001357 // Set the PCHLevel to filter out unwanted decls if requested.
1358 if (CXXUnit->getOnlyLocalDecls()) {
1359 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001360
Douglas Gregorb1373d02010-01-20 20:59:29 +00001361 // If the main input was an AST, bump the level.
1362 if (CXXUnit->isMainFileAST())
1363 ++PCHLevel;
1364 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001365
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001366 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001367 return CursorVis.VisitChildren(parent);
1368}
1369
Douglas Gregor78205d42010-01-20 21:45:58 +00001370static CXString getDeclSpelling(Decl *D) {
1371 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1372 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001373 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001374
Douglas Gregor78205d42010-01-20 21:45:58 +00001375 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001376 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001377
Douglas Gregor78205d42010-01-20 21:45:58 +00001378 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1379 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1380 // and returns different names. NamedDecl returns the class name and
1381 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001382 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001383
Douglas Gregor78205d42010-01-20 21:45:58 +00001384 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001385 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001386
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001387 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001388}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001389
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001390CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001391 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001392 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001393
Steve Narofff334b4e2009-09-02 18:26:48 +00001394 if (clang_isReference(C.kind)) {
1395 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001396 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001397 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001398 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001399 }
1400 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001401 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001402 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001403 }
1404 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001405 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001406 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001407 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001408 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001409 case CXCursor_TypeRef: {
1410 TypeDecl *Type = getCursorTypeRef(C).first;
1411 assert(Type && "Missing type decl");
1412
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001413 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1414 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001415 }
1416
Daniel Dunbaracca7252009-11-30 20:42:49 +00001417 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001418 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001419 }
1420 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001421
1422 if (clang_isExpression(C.kind)) {
1423 Decl *D = getDeclFromExpr(getCursorExpr(C));
1424 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001425 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001426 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001427 }
1428
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001429 if (clang_isDeclaration(C.kind))
1430 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001431
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001432 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001433}
1434
Ted Kremeneke68fff62010-02-17 00:41:32 +00001435CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001436 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001437 case CXCursor_FunctionDecl:
1438 return createCXString("FunctionDecl");
1439 case CXCursor_TypedefDecl:
1440 return createCXString("TypedefDecl");
1441 case CXCursor_EnumDecl:
1442 return createCXString("EnumDecl");
1443 case CXCursor_EnumConstantDecl:
1444 return createCXString("EnumConstantDecl");
1445 case CXCursor_StructDecl:
1446 return createCXString("StructDecl");
1447 case CXCursor_UnionDecl:
1448 return createCXString("UnionDecl");
1449 case CXCursor_ClassDecl:
1450 return createCXString("ClassDecl");
1451 case CXCursor_FieldDecl:
1452 return createCXString("FieldDecl");
1453 case CXCursor_VarDecl:
1454 return createCXString("VarDecl");
1455 case CXCursor_ParmDecl:
1456 return createCXString("ParmDecl");
1457 case CXCursor_ObjCInterfaceDecl:
1458 return createCXString("ObjCInterfaceDecl");
1459 case CXCursor_ObjCCategoryDecl:
1460 return createCXString("ObjCCategoryDecl");
1461 case CXCursor_ObjCProtocolDecl:
1462 return createCXString("ObjCProtocolDecl");
1463 case CXCursor_ObjCPropertyDecl:
1464 return createCXString("ObjCPropertyDecl");
1465 case CXCursor_ObjCIvarDecl:
1466 return createCXString("ObjCIvarDecl");
1467 case CXCursor_ObjCInstanceMethodDecl:
1468 return createCXString("ObjCInstanceMethodDecl");
1469 case CXCursor_ObjCClassMethodDecl:
1470 return createCXString("ObjCClassMethodDecl");
1471 case CXCursor_ObjCImplementationDecl:
1472 return createCXString("ObjCImplementationDecl");
1473 case CXCursor_ObjCCategoryImplDecl:
1474 return createCXString("ObjCCategoryImplDecl");
1475 case CXCursor_UnexposedDecl:
1476 return createCXString("UnexposedDecl");
1477 case CXCursor_ObjCSuperClassRef:
1478 return createCXString("ObjCSuperClassRef");
1479 case CXCursor_ObjCProtocolRef:
1480 return createCXString("ObjCProtocolRef");
1481 case CXCursor_ObjCClassRef:
1482 return createCXString("ObjCClassRef");
1483 case CXCursor_TypeRef:
1484 return createCXString("TypeRef");
1485 case CXCursor_UnexposedExpr:
1486 return createCXString("UnexposedExpr");
1487 case CXCursor_DeclRefExpr:
1488 return createCXString("DeclRefExpr");
1489 case CXCursor_MemberRefExpr:
1490 return createCXString("MemberRefExpr");
1491 case CXCursor_CallExpr:
1492 return createCXString("CallExpr");
1493 case CXCursor_ObjCMessageExpr:
1494 return createCXString("ObjCMessageExpr");
1495 case CXCursor_UnexposedStmt:
1496 return createCXString("UnexposedStmt");
1497 case CXCursor_InvalidFile:
1498 return createCXString("InvalidFile");
1499 case CXCursor_NoDeclFound:
1500 return createCXString("NoDeclFound");
1501 case CXCursor_NotImplemented:
1502 return createCXString("NotImplemented");
1503 case CXCursor_TranslationUnit:
1504 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001505 case CXCursor_UnexposedAttr:
1506 return createCXString("UnexposedAttr");
1507 case CXCursor_IBActionAttr:
1508 return createCXString("attribute(ibaction)");
1509 case CXCursor_IBOutletAttr:
1510 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001511 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001512
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001513 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001514 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001515}
Steve Naroff89922f82009-08-31 00:59:03 +00001516
Ted Kremeneke68fff62010-02-17 00:41:32 +00001517enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1518 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001519 CXClientData client_data) {
1520 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1521 *BestCursor = cursor;
1522 return CXChildVisit_Recurse;
1523}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001524
Douglas Gregorb9790342010-01-22 21:44:22 +00001525CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1526 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001527 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001528
Douglas Gregorb9790342010-01-22 21:44:22 +00001529 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1530
Douglas Gregorbdf60622010-03-05 21:16:25 +00001531 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1532
Ted Kremeneka297de22010-01-25 22:34:44 +00001533 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001534 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1535 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001536 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001537
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001538 // FIXME: Would be great to have a "hint" cursor, then walk from that
1539 // hint cursor upward until we find a cursor whose source range encloses
1540 // the region of interest, rather than starting from the translation unit.
1541 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001542 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001543 Decl::MaxPCHLevel, RegionOfInterest);
1544 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001545 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001546 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001547}
1548
Ted Kremenek73885552009-11-17 19:28:59 +00001549CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001550 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001551}
1552
1553unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001554 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001555}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001556
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001557unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001558 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1559}
1560
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001561unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001562 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1563}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001564
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001565unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001566 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1567}
1568
Douglas Gregor97b98722010-01-19 23:20:36 +00001569unsigned clang_isExpression(enum CXCursorKind K) {
1570 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1571}
1572
1573unsigned clang_isStatement(enum CXCursorKind K) {
1574 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1575}
1576
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001577unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1578 return K == CXCursor_TranslationUnit;
1579}
1580
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001581CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001582 return C.kind;
1583}
1584
Douglas Gregor98258af2010-01-18 22:46:11 +00001585CXSourceLocation clang_getCursorLocation(CXCursor C) {
1586 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001587 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001588 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001589 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1590 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001591 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001592 }
1593
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001594 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001595 std::pair<ObjCProtocolDecl *, SourceLocation> P
1596 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001597 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001598 }
1599
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001600 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001601 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1602 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001603 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001604 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001605
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001606 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001607 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001608 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001609 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001610
Douglas Gregorf46034a2010-01-18 23:41:10 +00001611 default:
1612 // FIXME: Need a way to enumerate all non-reference cases.
1613 llvm_unreachable("Missed a reference kind");
1614 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001615 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001616
1617 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001618 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001619 getLocationFromExpr(getCursorExpr(C)));
1620
Douglas Gregor5352ac02010-01-28 00:27:43 +00001621 if (!getCursorDecl(C))
1622 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001623
Douglas Gregorf46034a2010-01-18 23:41:10 +00001624 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001625 SourceLocation Loc = D->getLocation();
1626 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1627 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001628 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001629}
Douglas Gregora7bde202010-01-19 00:34:46 +00001630
1631CXSourceRange clang_getCursorExtent(CXCursor C) {
1632 if (clang_isReference(C.kind)) {
1633 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001634 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001635 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1636 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001637 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001638 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001639
1640 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001641 std::pair<ObjCProtocolDecl *, SourceLocation> P
1642 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001643 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001644 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001645
1646 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001647 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1648 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001649
Ted Kremeneka297de22010-01-25 22:34:44 +00001650 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001651 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001652
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001653 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001654 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001655 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001656 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001657
Douglas Gregora7bde202010-01-19 00:34:46 +00001658 default:
1659 // FIXME: Need a way to enumerate all non-reference cases.
1660 llvm_unreachable("Missed a reference kind");
1661 }
1662 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001663
1664 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001665 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001666 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001667
1668 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001669 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001670 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001671
Douglas Gregor5352ac02010-01-28 00:27:43 +00001672 if (!getCursorDecl(C))
1673 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001674
Douglas Gregora7bde202010-01-19 00:34:46 +00001675 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001676 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001677}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001678
1679CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001680 if (clang_isInvalid(C.kind))
1681 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001682
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001683 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001684 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001685 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001686
Douglas Gregor97b98722010-01-19 23:20:36 +00001687 if (clang_isExpression(C.kind)) {
1688 Decl *D = getDeclFromExpr(getCursorExpr(C));
1689 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001690 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001691 return clang_getNullCursor();
1692 }
1693
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001694 if (!clang_isReference(C.kind))
1695 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001696
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001697 switch (C.kind) {
1698 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001699 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001700
1701 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001702 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001703
1704 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001705 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001706
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001707 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001708 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001709
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001710 default:
1711 // We would prefer to enumerate all non-reference cursor kinds here.
1712 llvm_unreachable("Unhandled reference cursor kind");
1713 break;
1714 }
1715 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001716
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001717 return clang_getNullCursor();
1718}
1719
Douglas Gregorb6998662010-01-19 19:34:47 +00001720CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001721 if (clang_isInvalid(C.kind))
1722 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001723
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001724 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001725
Douglas Gregorb6998662010-01-19 19:34:47 +00001726 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001727 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001728 C = clang_getCursorReferenced(C);
1729 WasReference = true;
1730 }
1731
1732 if (!clang_isDeclaration(C.kind))
1733 return clang_getNullCursor();
1734
1735 Decl *D = getCursorDecl(C);
1736 if (!D)
1737 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001738
Douglas Gregorb6998662010-01-19 19:34:47 +00001739 switch (D->getKind()) {
1740 // Declaration kinds that don't really separate the notions of
1741 // declaration and definition.
1742 case Decl::Namespace:
1743 case Decl::Typedef:
1744 case Decl::TemplateTypeParm:
1745 case Decl::EnumConstant:
1746 case Decl::Field:
1747 case Decl::ObjCIvar:
1748 case Decl::ObjCAtDefsField:
1749 case Decl::ImplicitParam:
1750 case Decl::ParmVar:
1751 case Decl::NonTypeTemplateParm:
1752 case Decl::TemplateTemplateParm:
1753 case Decl::ObjCCategoryImpl:
1754 case Decl::ObjCImplementation:
1755 case Decl::LinkageSpec:
1756 case Decl::ObjCPropertyImpl:
1757 case Decl::FileScopeAsm:
1758 case Decl::StaticAssert:
1759 case Decl::Block:
1760 return C;
1761
1762 // Declaration kinds that don't make any sense here, but are
1763 // nonetheless harmless.
1764 case Decl::TranslationUnit:
1765 case Decl::Template:
1766 case Decl::ObjCContainer:
1767 break;
1768
1769 // Declaration kinds for which the definition is not resolvable.
1770 case Decl::UnresolvedUsingTypename:
1771 case Decl::UnresolvedUsingValue:
1772 break;
1773
1774 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001775 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1776 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001777
1778 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001779 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001780
1781 case Decl::Enum:
1782 case Decl::Record:
1783 case Decl::CXXRecord:
1784 case Decl::ClassTemplateSpecialization:
1785 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001786 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001787 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001788 return clang_getNullCursor();
1789
1790 case Decl::Function:
1791 case Decl::CXXMethod:
1792 case Decl::CXXConstructor:
1793 case Decl::CXXDestructor:
1794 case Decl::CXXConversion: {
1795 const FunctionDecl *Def = 0;
1796 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001797 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001798 return clang_getNullCursor();
1799 }
1800
1801 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001802 // Ask the variable if it has a definition.
1803 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1804 return MakeCXCursor(Def, CXXUnit);
1805 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001806 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001807
Douglas Gregorb6998662010-01-19 19:34:47 +00001808 case Decl::FunctionTemplate: {
1809 const FunctionDecl *Def = 0;
1810 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001811 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001812 return clang_getNullCursor();
1813 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001814
Douglas Gregorb6998662010-01-19 19:34:47 +00001815 case Decl::ClassTemplate: {
1816 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001817 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001818 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001819 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001820 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001821 return clang_getNullCursor();
1822 }
1823
1824 case Decl::Using: {
1825 UsingDecl *Using = cast<UsingDecl>(D);
1826 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001827 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1828 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001829 S != SEnd; ++S) {
1830 if (Def != clang_getNullCursor()) {
1831 // FIXME: We have no way to return multiple results.
1832 return clang_getNullCursor();
1833 }
1834
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001835 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001836 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001837 }
1838
1839 return Def;
1840 }
1841
1842 case Decl::UsingShadow:
1843 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001844 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001845 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001846
1847 case Decl::ObjCMethod: {
1848 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1849 if (Method->isThisDeclarationADefinition())
1850 return C;
1851
1852 // Dig out the method definition in the associated
1853 // @implementation, if we have it.
1854 // FIXME: The ASTs should make finding the definition easier.
1855 if (ObjCInterfaceDecl *Class
1856 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1857 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1858 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1859 Method->isInstanceMethod()))
1860 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001861 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001862
1863 return clang_getNullCursor();
1864 }
1865
1866 case Decl::ObjCCategory:
1867 if (ObjCCategoryImplDecl *Impl
1868 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001869 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001870 return clang_getNullCursor();
1871
1872 case Decl::ObjCProtocol:
1873 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1874 return C;
1875 return clang_getNullCursor();
1876
1877 case Decl::ObjCInterface:
1878 // There are two notions of a "definition" for an Objective-C
1879 // class: the interface and its implementation. When we resolved a
1880 // reference to an Objective-C class, produce the @interface as
1881 // the definition; when we were provided with the interface,
1882 // produce the @implementation as the definition.
1883 if (WasReference) {
1884 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1885 return C;
1886 } else if (ObjCImplementationDecl *Impl
1887 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001888 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001889 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001890
Douglas Gregorb6998662010-01-19 19:34:47 +00001891 case Decl::ObjCProperty:
1892 // FIXME: We don't really know where to find the
1893 // ObjCPropertyImplDecls that implement this property.
1894 return clang_getNullCursor();
1895
1896 case Decl::ObjCCompatibleAlias:
1897 if (ObjCInterfaceDecl *Class
1898 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1899 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001900 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001901
Douglas Gregorb6998662010-01-19 19:34:47 +00001902 return clang_getNullCursor();
1903
1904 case Decl::ObjCForwardProtocol: {
1905 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1906 if (Forward->protocol_size() == 1)
1907 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001908 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001909 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001910
1911 // FIXME: Cannot return multiple definitions.
1912 return clang_getNullCursor();
1913 }
1914
1915 case Decl::ObjCClass: {
1916 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1917 if (Class->size() == 1) {
1918 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1919 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001920 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001921 return clang_getNullCursor();
1922 }
1923
1924 // FIXME: Cannot return multiple definitions.
1925 return clang_getNullCursor();
1926 }
1927
1928 case Decl::Friend:
1929 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001930 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001931 return clang_getNullCursor();
1932
1933 case Decl::FriendTemplate:
1934 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001935 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001936 return clang_getNullCursor();
1937 }
1938
1939 return clang_getNullCursor();
1940}
1941
1942unsigned clang_isCursorDefinition(CXCursor C) {
1943 if (!clang_isDeclaration(C.kind))
1944 return 0;
1945
1946 return clang_getCursorDefinition(C) == C;
1947}
1948
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001949void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001950 const char **startBuf,
1951 const char **endBuf,
1952 unsigned *startLine,
1953 unsigned *startColumn,
1954 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001955 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001956 assert(getCursorDecl(C) && "CXCursor has null decl");
1957 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001958 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1959 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001960
Steve Naroff4ade6d62009-09-23 17:52:52 +00001961 SourceManager &SM = FD->getASTContext().getSourceManager();
1962 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1963 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1964 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1965 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1966 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1967 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1968}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001969
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001970void clang_enableStackTraces(void) {
1971 llvm::sys::PrintStackTraceOnErrorSignal();
1972}
1973
Ted Kremenekfb480492010-01-13 21:46:36 +00001974} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001975
Ted Kremenekfb480492010-01-13 21:46:36 +00001976//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001977// Token-based Operations.
1978//===----------------------------------------------------------------------===//
1979
1980/* CXToken layout:
1981 * int_data[0]: a CXTokenKind
1982 * int_data[1]: starting token location
1983 * int_data[2]: token length
1984 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001985 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001986 * otherwise unused.
1987 */
1988extern "C" {
1989
1990CXTokenKind clang_getTokenKind(CXToken CXTok) {
1991 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1992}
1993
1994CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1995 switch (clang_getTokenKind(CXTok)) {
1996 case CXToken_Identifier:
1997 case CXToken_Keyword:
1998 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001999 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2000 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002001
2002 case CXToken_Literal: {
2003 // We have stashed the starting pointer in the ptr_data field. Use it.
2004 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002005 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002006 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002007
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002008 case CXToken_Punctuation:
2009 case CXToken_Comment:
2010 break;
2011 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002012
2013 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002014 // deconstructing the source location.
2015 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2016 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002017 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002018
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002019 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2020 std::pair<FileID, unsigned> LocInfo
2021 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
2022 std::pair<const char *,const char *> Buffer
2023 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
2024
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002025 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
2026 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002027}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002028
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002029CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2030 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2031 if (!CXXUnit)
2032 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002033
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002034 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2035 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2036}
2037
2038CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2039 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002040 if (!CXXUnit)
2041 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002042
2043 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002044 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2045}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002046
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002047void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2048 CXToken **Tokens, unsigned *NumTokens) {
2049 if (Tokens)
2050 *Tokens = 0;
2051 if (NumTokens)
2052 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002053
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002054 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2055 if (!CXXUnit || !Tokens || !NumTokens)
2056 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002057
Douglas Gregorbdf60622010-03-05 21:16:25 +00002058 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2059
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002060 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002061 if (R.isInvalid())
2062 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002063
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002064 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2065 std::pair<FileID, unsigned> BeginLocInfo
2066 = SourceMgr.getDecomposedLoc(R.getBegin());
2067 std::pair<FileID, unsigned> EndLocInfo
2068 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002069
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002070 // Cannot tokenize across files.
2071 if (BeginLocInfo.first != EndLocInfo.first)
2072 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002073
2074 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002075 std::pair<const char *,const char *> Buffer
2076 = SourceMgr.getBufferData(BeginLocInfo.first);
2077 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2078 CXXUnit->getASTContext().getLangOptions(),
2079 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2080 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002081
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002082 // Lex tokens until we hit the end of the range.
2083 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2084 llvm::SmallVector<CXToken, 32> CXTokens;
2085 Token Tok;
2086 do {
2087 // Lex the next token
2088 Lex.LexFromRawLexer(Tok);
2089 if (Tok.is(tok::eof))
2090 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002091
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002092 // Initialize the CXToken.
2093 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002094
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002095 // - Common fields
2096 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2097 CXTok.int_data[2] = Tok.getLength();
2098 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002099
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002100 // - Kind-specific fields
2101 if (Tok.isLiteral()) {
2102 CXTok.int_data[0] = CXToken_Literal;
2103 CXTok.ptr_data = (void *)Tok.getLiteralData();
2104 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002105 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002106 std::pair<FileID, unsigned> LocInfo
2107 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002108 const char *StartPos
2109 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002110 LocInfo.second;
2111 IdentifierInfo *II
2112 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2113 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2114 CXToken_Identifier
2115 : CXToken_Keyword;
2116 CXTok.ptr_data = II;
2117 } else if (Tok.is(tok::comment)) {
2118 CXTok.int_data[0] = CXToken_Comment;
2119 CXTok.ptr_data = 0;
2120 } else {
2121 CXTok.int_data[0] = CXToken_Punctuation;
2122 CXTok.ptr_data = 0;
2123 }
2124 CXTokens.push_back(CXTok);
2125 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002126
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002127 if (CXTokens.empty())
2128 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002129
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002130 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2131 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2132 *NumTokens = CXTokens.size();
2133}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002134
2135typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2136
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002137enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2138 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002139 CXClientData client_data) {
2140 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2141
2142 // We only annotate the locations of declarations, simple
2143 // references, and expressions which directly reference something.
2144 CXCursorKind Kind = clang_getCursorKind(cursor);
2145 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2146 // Okay: We can annotate the location of this declaration with the
2147 // declaration or reference
2148 } else if (clang_isExpression(cursor.kind)) {
2149 if (Kind != CXCursor_DeclRefExpr &&
2150 Kind != CXCursor_MemberRefExpr &&
2151 Kind != CXCursor_ObjCMessageExpr)
2152 return CXChildVisit_Recurse;
2153
2154 CXCursor Referenced = clang_getCursorReferenced(cursor);
2155 if (Referenced == cursor || Referenced == clang_getNullCursor())
2156 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002157
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002158 // Okay: we can annotate the location of this expression
2159 } else {
2160 // Nothing to annotate
2161 return CXChildVisit_Recurse;
2162 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002163
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002164 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2165 (*Data)[Loc.int_data] = cursor;
2166 return CXChildVisit_Recurse;
2167}
2168
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002169void clang_annotateTokens(CXTranslationUnit TU,
2170 CXToken *Tokens, unsigned NumTokens,
2171 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002172 if (NumTokens == 0)
2173 return;
2174
2175 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002176 for (unsigned I = 0; I != NumTokens; ++I)
2177 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002178
2179 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2180 if (!CXXUnit || !Tokens)
2181 return;
2182
Douglas Gregorbdf60622010-03-05 21:16:25 +00002183 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2184
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002185 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002186 SourceRange RegionOfInterest;
2187 RegionOfInterest.setBegin(
2188 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2189 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002190 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002191 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002192 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002193 // FIXME: Would be great to have a "hint" cursor, then walk from that
2194 // hint cursor upward until we find a cursor whose source range encloses
2195 // the region of interest, rather than starting from the translation unit.
2196 AnnotateTokensData Annotated;
2197 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002198 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002199 Decl::MaxPCHLevel, RegionOfInterest);
2200 AnnotateVis.VisitChildren(Parent);
2201
2202 for (unsigned I = 0; I != NumTokens; ++I) {
2203 // Determine whether we saw a cursor at this token's location.
2204 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2205 if (Pos == Annotated.end())
2206 continue;
2207
2208 Cursors[I] = Pos->second;
2209 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002210}
2211
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002212void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002213 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002214 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002215}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002216
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002217} // end: extern "C"
2218
2219//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002220// Operations for querying linkage of a cursor.
2221//===----------------------------------------------------------------------===//
2222
2223extern "C" {
2224CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
2225 Decl *D = cxcursor::getCursorDecl(cursor);
2226 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2227 switch (ND->getLinkage()) {
2228 case NoLinkage: return CXLinkage_NoLinkage;
2229 case InternalLinkage: return CXLinkage_Internal;
2230 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2231 case ExternalLinkage: return CXLinkage_External;
2232 };
2233
2234 return CXLinkage_Invalid;
2235}
2236} // end: extern "C"
2237
2238//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002239// CXString Operations.
2240//===----------------------------------------------------------------------===//
2241
2242extern "C" {
2243const char *clang_getCString(CXString string) {
2244 return string.Spelling;
2245}
2246
2247void clang_disposeString(CXString string) {
2248 if (string.MustFreeString && string.Spelling)
2249 free((void*)string.Spelling);
2250}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002251
Ted Kremenekfb480492010-01-13 21:46:36 +00002252} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002253
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002254namespace clang { namespace cxstring {
2255CXString createCXString(const char *String, bool DupString){
2256 CXString Str;
2257 if (DupString) {
2258 Str.Spelling = strdup(String);
2259 Str.MustFreeString = 1;
2260 } else {
2261 Str.Spelling = String;
2262 Str.MustFreeString = 0;
2263 }
2264 return Str;
2265}
2266
2267CXString createCXString(llvm::StringRef String, bool DupString) {
2268 CXString Result;
2269 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2270 char *Spelling = (char *)malloc(String.size() + 1);
2271 memmove(Spelling, String.data(), String.size());
2272 Spelling[String.size()] = 0;
2273 Result.Spelling = Spelling;
2274 Result.MustFreeString = 1;
2275 } else {
2276 Result.Spelling = String.data();
2277 Result.MustFreeString = 0;
2278 }
2279 return Result;
2280}
2281}}
2282
Ted Kremenek04bb7162010-01-22 22:44:15 +00002283//===----------------------------------------------------------------------===//
2284// Misc. utility functions.
2285//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002286
Ted Kremenek04bb7162010-01-22 22:44:15 +00002287extern "C" {
2288
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002289CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002290 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002291}
2292
2293} // end: extern "C"