blob: 07bb7fbd83efc331169d6767a1136c9dba03818a [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()) {
Ted Kremenek34f6a322010-03-05 22:43:29 +00001006 // Make sure to check that 'Unit' is non-NULL.
1007 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001008 for (ASTUnit::diag_iterator D = Unit->diag_begin(),
1009 DEnd = Unit->diag_end();
1010 D != DEnd; ++D) {
1011 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001012 CXString Msg = clang_formatDiagnostic(&Diag,
1013 clang_defaultDiagnosticDisplayOptions());
1014 fprintf(stderr, "%s\n", clang_getCString(Msg));
1015 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001016 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001017#ifdef LLVM_ON_WIN32
1018 // On Windows, force a flush, since there may be multiple copies of
1019 // stderr and stdout in the file system, all with different buffers
1020 // but writing to the same device.
1021 fflush(stderr);
1022#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001023 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001024 return 0;
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001025 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001026
1027 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001028 }
1029
Ted Kremenek139ba862009-10-22 00:03:57 +00001030 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001031 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001032
Ted Kremenek139ba862009-10-22 00:03:57 +00001033 // First add the complete path to the 'clang' executable.
1034 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001035 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001036
Ted Kremenek139ba862009-10-22 00:03:57 +00001037 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001038 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001039
Ted Kremenek139ba862009-10-22 00:03:57 +00001040 // The 'source_filename' argument is optional. If the caller does not
1041 // specify it then it is assumed that the source file is specified
1042 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001043 if (source_filename)
1044 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001045
Steve Naroff37b5ac22009-10-15 20:50:09 +00001046 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001047 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001048 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001049 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001050
Douglas Gregor4db64a42010-01-23 00:14:00 +00001051 // Remap any unsaved files to temporary files.
1052 std::vector<llvm::sys::Path> TemporaryFiles;
1053 std::vector<std::string> RemapArgs;
1054 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1055 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001056
Douglas Gregor4db64a42010-01-23 00:14:00 +00001057 // The pointers into the elements of RemapArgs are stable because we
1058 // won't be adding anything to RemapArgs after this point.
1059 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1060 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001061
Ted Kremenek139ba862009-10-22 00:03:57 +00001062 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1063 for (int i = 0; i < num_command_line_args; ++i)
1064 if (const char *arg = command_line_args[i]) {
1065 if (strcmp(arg, "-o") == 0) {
1066 ++i; // Also skip the matching argument.
1067 continue;
1068 }
1069 if (strcmp(arg, "-emit-ast") == 0 ||
1070 strcmp(arg, "-c") == 0 ||
1071 strcmp(arg, "-fsyntax-only") == 0) {
1072 continue;
1073 }
1074
1075 // Keep the argument.
1076 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001077 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001078
Douglas Gregord93256e2010-01-28 06:00:51 +00001079 // Generate a temporary name for the diagnostics file.
1080 char tmpFileResults[L_tmpnam];
1081 char *tmpResultsFileName = tmpnam(tmpFileResults);
1082 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1083 TemporaryFiles.push_back(DiagnosticsFile);
1084 argv.push_back("-fdiagnostics-binary");
1085
Ted Kremenek139ba862009-10-22 00:03:57 +00001086 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001087 argv.push_back(NULL);
1088
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001089 // Invoke 'clang'.
1090 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1091 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001092 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001093 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1094 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001095 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001096 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001097 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001098
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001099 if (!ErrMsg.empty()) {
1100 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001101 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001102 I != E; ++I) {
1103 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001104 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001105 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001106 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001107
Daniel Dunbar32141c82010-02-23 20:23:45 +00001108 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001109 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001110
Douglas Gregor5352ac02010-01-28 00:27:43 +00001111 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001112 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001113 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001114 RemappedFiles.size(),
1115 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001116 if (ATU) {
1117 LoadSerializedDiagnostics(DiagnosticsFile,
1118 num_unsaved_files, unsaved_files,
1119 ATU->getFileManager(),
1120 ATU->getSourceManager(),
1121 ATU->getDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001122 } else if (CXXIdx->getDisplayDiagnostics()) {
1123 // We failed to load the ASTUnit, but we can still deserialize the
1124 // diagnostics and emit them.
1125 FileManager FileMgr;
1126 SourceManager SourceMgr;
1127 // FIXME: Faked LangOpts!
1128 LangOptions LangOpts;
1129 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1130 LoadSerializedDiagnostics(DiagnosticsFile,
1131 num_unsaved_files, unsaved_files,
1132 FileMgr, SourceMgr, Diags);
1133 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1134 DEnd = Diags.end();
1135 D != DEnd; ++D) {
1136 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001137 CXString Msg = clang_formatDiagnostic(&Diag,
1138 clang_defaultDiagnosticDisplayOptions());
1139 fprintf(stderr, "%s\n", clang_getCString(Msg));
1140 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001141 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001142
1143#ifdef LLVM_ON_WIN32
1144 // On Windows, force a flush, since there may be multiple copies of
1145 // stderr and stdout in the file system, all with different buffers
1146 // but writing to the same device.
1147 fflush(stderr);
1148#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001149 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001150
Douglas Gregor313e26c2010-02-18 23:35:40 +00001151 if (ATU) {
1152 // Make the translation unit responsible for destroying all temporary files.
1153 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1154 ATU->addTemporaryFile(TemporaryFiles[i]);
1155 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1156 } else {
1157 // Destroy all of the temporary files now; they can't be referenced any
1158 // longer.
1159 llvm::sys::Path(astTmpFile).eraseFromDisk();
1160 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1161 TemporaryFiles[i].eraseFromDisk();
1162 }
1163
Steve Naroffe19944c2009-10-15 22:23:48 +00001164 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001165}
1166
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001167void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001168 if (CTUnit)
1169 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001170}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001171
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001172CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001173 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001174 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001175
Steve Naroff77accc12009-09-03 18:19:54 +00001176 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001177 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001178}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001179
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001180CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001181 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001182 return Result;
1183}
1184
Ted Kremenekfb480492010-01-13 21:46:36 +00001185} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001186
Ted Kremenekfb480492010-01-13 21:46:36 +00001187//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001188// CXSourceLocation and CXSourceRange Operations.
1189//===----------------------------------------------------------------------===//
1190
Douglas Gregorb9790342010-01-22 21:44:22 +00001191extern "C" {
1192CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001193 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001194 return Result;
1195}
1196
1197unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001198 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1199 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1200 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001201}
1202
1203CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1204 CXFile file,
1205 unsigned line,
1206 unsigned column) {
1207 if (!tu)
1208 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001209
Douglas Gregorb9790342010-01-22 21:44:22 +00001210 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1211 SourceLocation SLoc
1212 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001213 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001214 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001215
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001216 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001217}
1218
Douglas Gregor5352ac02010-01-28 00:27:43 +00001219CXSourceRange clang_getNullRange() {
1220 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1221 return Result;
1222}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001223
Douglas Gregor5352ac02010-01-28 00:27:43 +00001224CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1225 if (begin.ptr_data[0] != end.ptr_data[0] ||
1226 begin.ptr_data[1] != end.ptr_data[1])
1227 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001228
1229 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001230 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001231 return Result;
1232}
1233
Douglas Gregor46766dc2010-01-26 19:19:08 +00001234void clang_getInstantiationLocation(CXSourceLocation location,
1235 CXFile *file,
1236 unsigned *line,
1237 unsigned *column,
1238 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001239 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1240
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001241 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001242 if (file)
1243 *file = 0;
1244 if (line)
1245 *line = 0;
1246 if (column)
1247 *column = 0;
1248 if (offset)
1249 *offset = 0;
1250 return;
1251 }
1252
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001253 const SourceManager &SM =
1254 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001255 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001256
1257 if (file)
1258 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1259 if (line)
1260 *line = SM.getInstantiationLineNumber(InstLoc);
1261 if (column)
1262 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001263 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001264 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001265}
1266
Douglas Gregor1db19de2010-01-19 21:36:55 +00001267CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001268 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001269 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001270 return Result;
1271}
1272
1273CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001274 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001275 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001276 return Result;
1277}
1278
Douglas Gregorb9790342010-01-22 21:44:22 +00001279} // end: extern "C"
1280
Douglas Gregor1db19de2010-01-19 21:36:55 +00001281//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001282// CXFile Operations.
1283//===----------------------------------------------------------------------===//
1284
1285extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001286CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001287 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001288 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001289
Steve Naroff88145032009-10-27 14:35:18 +00001290 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001291 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001292}
1293
1294time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001295 if (!SFile)
1296 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001297
Steve Naroff88145032009-10-27 14:35:18 +00001298 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1299 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001300}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001301
Douglas Gregorb9790342010-01-22 21:44:22 +00001302CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1303 if (!tu)
1304 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001305
Douglas Gregorb9790342010-01-22 21:44:22 +00001306 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001307
Douglas Gregorb9790342010-01-22 21:44:22 +00001308 FileManager &FMgr = CXXUnit->getFileManager();
1309 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1310 return const_cast<FileEntry *>(File);
1311}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001312
Ted Kremenekfb480492010-01-13 21:46:36 +00001313} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001314
Ted Kremenekfb480492010-01-13 21:46:36 +00001315//===----------------------------------------------------------------------===//
1316// CXCursor Operations.
1317//===----------------------------------------------------------------------===//
1318
Ted Kremenekfb480492010-01-13 21:46:36 +00001319static Decl *getDeclFromExpr(Stmt *E) {
1320 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1321 return RefExpr->getDecl();
1322 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1323 return ME->getMemberDecl();
1324 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1325 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001326
Ted Kremenekfb480492010-01-13 21:46:36 +00001327 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1328 return getDeclFromExpr(CE->getCallee());
1329 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1330 return getDeclFromExpr(CE->getSubExpr());
1331 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1332 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001333
Ted Kremenekfb480492010-01-13 21:46:36 +00001334 return 0;
1335}
1336
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001337static SourceLocation getLocationFromExpr(Expr *E) {
1338 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1339 return /*FIXME:*/Msg->getLeftLoc();
1340 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1341 return DRE->getLocation();
1342 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1343 return Member->getMemberLoc();
1344 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1345 return Ivar->getLocation();
1346 return E->getLocStart();
1347}
1348
Ted Kremenekfb480492010-01-13 21:46:36 +00001349extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001350
1351unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001352 CXCursorVisitor visitor,
1353 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001354 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001355
1356 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001357
Douglas Gregorb1373d02010-01-20 20:59:29 +00001358 // Set the PCHLevel to filter out unwanted decls if requested.
1359 if (CXXUnit->getOnlyLocalDecls()) {
1360 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001361
Douglas Gregorb1373d02010-01-20 20:59:29 +00001362 // If the main input was an AST, bump the level.
1363 if (CXXUnit->isMainFileAST())
1364 ++PCHLevel;
1365 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001366
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001367 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001368 return CursorVis.VisitChildren(parent);
1369}
1370
Douglas Gregor78205d42010-01-20 21:45:58 +00001371static CXString getDeclSpelling(Decl *D) {
1372 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1373 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001374 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001375
Douglas Gregor78205d42010-01-20 21:45:58 +00001376 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001377 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001378
Douglas Gregor78205d42010-01-20 21:45:58 +00001379 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1380 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1381 // and returns different names. NamedDecl returns the class name and
1382 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001383 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001384
Douglas Gregor78205d42010-01-20 21:45:58 +00001385 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001386 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001387
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001388 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001389}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001390
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001391CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001392 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001393 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001394
Steve Narofff334b4e2009-09-02 18:26:48 +00001395 if (clang_isReference(C.kind)) {
1396 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001397 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001398 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001399 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001400 }
1401 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001402 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001403 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001404 }
1405 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001406 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001407 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001408 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001409 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001410 case CXCursor_TypeRef: {
1411 TypeDecl *Type = getCursorTypeRef(C).first;
1412 assert(Type && "Missing type decl");
1413
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001414 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1415 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001416 }
1417
Daniel Dunbaracca7252009-11-30 20:42:49 +00001418 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001419 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001420 }
1421 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001422
1423 if (clang_isExpression(C.kind)) {
1424 Decl *D = getDeclFromExpr(getCursorExpr(C));
1425 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001426 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001427 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001428 }
1429
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001430 if (clang_isDeclaration(C.kind))
1431 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001432
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001433 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001434}
1435
Ted Kremeneke68fff62010-02-17 00:41:32 +00001436CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001437 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001438 case CXCursor_FunctionDecl:
1439 return createCXString("FunctionDecl");
1440 case CXCursor_TypedefDecl:
1441 return createCXString("TypedefDecl");
1442 case CXCursor_EnumDecl:
1443 return createCXString("EnumDecl");
1444 case CXCursor_EnumConstantDecl:
1445 return createCXString("EnumConstantDecl");
1446 case CXCursor_StructDecl:
1447 return createCXString("StructDecl");
1448 case CXCursor_UnionDecl:
1449 return createCXString("UnionDecl");
1450 case CXCursor_ClassDecl:
1451 return createCXString("ClassDecl");
1452 case CXCursor_FieldDecl:
1453 return createCXString("FieldDecl");
1454 case CXCursor_VarDecl:
1455 return createCXString("VarDecl");
1456 case CXCursor_ParmDecl:
1457 return createCXString("ParmDecl");
1458 case CXCursor_ObjCInterfaceDecl:
1459 return createCXString("ObjCInterfaceDecl");
1460 case CXCursor_ObjCCategoryDecl:
1461 return createCXString("ObjCCategoryDecl");
1462 case CXCursor_ObjCProtocolDecl:
1463 return createCXString("ObjCProtocolDecl");
1464 case CXCursor_ObjCPropertyDecl:
1465 return createCXString("ObjCPropertyDecl");
1466 case CXCursor_ObjCIvarDecl:
1467 return createCXString("ObjCIvarDecl");
1468 case CXCursor_ObjCInstanceMethodDecl:
1469 return createCXString("ObjCInstanceMethodDecl");
1470 case CXCursor_ObjCClassMethodDecl:
1471 return createCXString("ObjCClassMethodDecl");
1472 case CXCursor_ObjCImplementationDecl:
1473 return createCXString("ObjCImplementationDecl");
1474 case CXCursor_ObjCCategoryImplDecl:
1475 return createCXString("ObjCCategoryImplDecl");
1476 case CXCursor_UnexposedDecl:
1477 return createCXString("UnexposedDecl");
1478 case CXCursor_ObjCSuperClassRef:
1479 return createCXString("ObjCSuperClassRef");
1480 case CXCursor_ObjCProtocolRef:
1481 return createCXString("ObjCProtocolRef");
1482 case CXCursor_ObjCClassRef:
1483 return createCXString("ObjCClassRef");
1484 case CXCursor_TypeRef:
1485 return createCXString("TypeRef");
1486 case CXCursor_UnexposedExpr:
1487 return createCXString("UnexposedExpr");
1488 case CXCursor_DeclRefExpr:
1489 return createCXString("DeclRefExpr");
1490 case CXCursor_MemberRefExpr:
1491 return createCXString("MemberRefExpr");
1492 case CXCursor_CallExpr:
1493 return createCXString("CallExpr");
1494 case CXCursor_ObjCMessageExpr:
1495 return createCXString("ObjCMessageExpr");
1496 case CXCursor_UnexposedStmt:
1497 return createCXString("UnexposedStmt");
1498 case CXCursor_InvalidFile:
1499 return createCXString("InvalidFile");
1500 case CXCursor_NoDeclFound:
1501 return createCXString("NoDeclFound");
1502 case CXCursor_NotImplemented:
1503 return createCXString("NotImplemented");
1504 case CXCursor_TranslationUnit:
1505 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001506 case CXCursor_UnexposedAttr:
1507 return createCXString("UnexposedAttr");
1508 case CXCursor_IBActionAttr:
1509 return createCXString("attribute(ibaction)");
1510 case CXCursor_IBOutletAttr:
1511 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001512 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001513
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001514 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001515 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001516}
Steve Naroff89922f82009-08-31 00:59:03 +00001517
Ted Kremeneke68fff62010-02-17 00:41:32 +00001518enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1519 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001520 CXClientData client_data) {
1521 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1522 *BestCursor = cursor;
1523 return CXChildVisit_Recurse;
1524}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001525
Douglas Gregorb9790342010-01-22 21:44:22 +00001526CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1527 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001528 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001529
Douglas Gregorb9790342010-01-22 21:44:22 +00001530 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1531
Douglas Gregorbdf60622010-03-05 21:16:25 +00001532 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1533
Ted Kremeneka297de22010-01-25 22:34:44 +00001534 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001535 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1536 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001537 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001538
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001539 // FIXME: Would be great to have a "hint" cursor, then walk from that
1540 // hint cursor upward until we find a cursor whose source range encloses
1541 // the region of interest, rather than starting from the translation unit.
1542 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001543 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001544 Decl::MaxPCHLevel, RegionOfInterest);
1545 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001546 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001547 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001548}
1549
Ted Kremenek73885552009-11-17 19:28:59 +00001550CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001551 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001552}
1553
1554unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001555 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001556}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001557
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001558unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001559 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1560}
1561
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001562unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001563 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1564}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001565
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001566unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001567 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1568}
1569
Douglas Gregor97b98722010-01-19 23:20:36 +00001570unsigned clang_isExpression(enum CXCursorKind K) {
1571 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1572}
1573
1574unsigned clang_isStatement(enum CXCursorKind K) {
1575 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1576}
1577
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001578unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1579 return K == CXCursor_TranslationUnit;
1580}
1581
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001582CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001583 return C.kind;
1584}
1585
Douglas Gregor98258af2010-01-18 22:46:11 +00001586CXSourceLocation clang_getCursorLocation(CXCursor C) {
1587 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001588 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001589 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001590 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1591 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001592 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001593 }
1594
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001595 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001596 std::pair<ObjCProtocolDecl *, SourceLocation> P
1597 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001598 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001599 }
1600
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001601 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001602 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1603 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001604 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001605 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001606
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001607 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001608 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001609 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001610 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001611
Douglas Gregorf46034a2010-01-18 23:41:10 +00001612 default:
1613 // FIXME: Need a way to enumerate all non-reference cases.
1614 llvm_unreachable("Missed a reference kind");
1615 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001616 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001617
1618 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001619 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001620 getLocationFromExpr(getCursorExpr(C)));
1621
Douglas Gregor5352ac02010-01-28 00:27:43 +00001622 if (!getCursorDecl(C))
1623 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001624
Douglas Gregorf46034a2010-01-18 23:41:10 +00001625 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001626 SourceLocation Loc = D->getLocation();
1627 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1628 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001629 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001630}
Douglas Gregora7bde202010-01-19 00:34:46 +00001631
1632CXSourceRange clang_getCursorExtent(CXCursor C) {
1633 if (clang_isReference(C.kind)) {
1634 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001635 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001636 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1637 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001638 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001639 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001640
1641 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001642 std::pair<ObjCProtocolDecl *, SourceLocation> P
1643 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001644 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001645 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001646
1647 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001648 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1649 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001650
Ted Kremeneka297de22010-01-25 22:34:44 +00001651 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001652 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001653
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001654 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001655 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001656 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001657 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001658
Douglas Gregora7bde202010-01-19 00:34:46 +00001659 default:
1660 // FIXME: Need a way to enumerate all non-reference cases.
1661 llvm_unreachable("Missed a reference kind");
1662 }
1663 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001664
1665 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001666 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001667 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001668
1669 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001670 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001671 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001672
Douglas Gregor5352ac02010-01-28 00:27:43 +00001673 if (!getCursorDecl(C))
1674 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001675
Douglas Gregora7bde202010-01-19 00:34:46 +00001676 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001677 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001678}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001679
1680CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001681 if (clang_isInvalid(C.kind))
1682 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001683
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001684 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001685 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001686 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001687
Douglas Gregor97b98722010-01-19 23:20:36 +00001688 if (clang_isExpression(C.kind)) {
1689 Decl *D = getDeclFromExpr(getCursorExpr(C));
1690 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001691 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001692 return clang_getNullCursor();
1693 }
1694
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001695 if (!clang_isReference(C.kind))
1696 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001697
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001698 switch (C.kind) {
1699 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001700 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001701
1702 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001703 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001704
1705 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001706 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001707
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001708 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001709 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001710
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001711 default:
1712 // We would prefer to enumerate all non-reference cursor kinds here.
1713 llvm_unreachable("Unhandled reference cursor kind");
1714 break;
1715 }
1716 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001717
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001718 return clang_getNullCursor();
1719}
1720
Douglas Gregorb6998662010-01-19 19:34:47 +00001721CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001722 if (clang_isInvalid(C.kind))
1723 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001724
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001725 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001726
Douglas Gregorb6998662010-01-19 19:34:47 +00001727 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001728 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001729 C = clang_getCursorReferenced(C);
1730 WasReference = true;
1731 }
1732
1733 if (!clang_isDeclaration(C.kind))
1734 return clang_getNullCursor();
1735
1736 Decl *D = getCursorDecl(C);
1737 if (!D)
1738 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001739
Douglas Gregorb6998662010-01-19 19:34:47 +00001740 switch (D->getKind()) {
1741 // Declaration kinds that don't really separate the notions of
1742 // declaration and definition.
1743 case Decl::Namespace:
1744 case Decl::Typedef:
1745 case Decl::TemplateTypeParm:
1746 case Decl::EnumConstant:
1747 case Decl::Field:
1748 case Decl::ObjCIvar:
1749 case Decl::ObjCAtDefsField:
1750 case Decl::ImplicitParam:
1751 case Decl::ParmVar:
1752 case Decl::NonTypeTemplateParm:
1753 case Decl::TemplateTemplateParm:
1754 case Decl::ObjCCategoryImpl:
1755 case Decl::ObjCImplementation:
1756 case Decl::LinkageSpec:
1757 case Decl::ObjCPropertyImpl:
1758 case Decl::FileScopeAsm:
1759 case Decl::StaticAssert:
1760 case Decl::Block:
1761 return C;
1762
1763 // Declaration kinds that don't make any sense here, but are
1764 // nonetheless harmless.
1765 case Decl::TranslationUnit:
1766 case Decl::Template:
1767 case Decl::ObjCContainer:
1768 break;
1769
1770 // Declaration kinds for which the definition is not resolvable.
1771 case Decl::UnresolvedUsingTypename:
1772 case Decl::UnresolvedUsingValue:
1773 break;
1774
1775 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001776 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1777 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001778
1779 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001780 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001781
1782 case Decl::Enum:
1783 case Decl::Record:
1784 case Decl::CXXRecord:
1785 case Decl::ClassTemplateSpecialization:
1786 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001787 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001788 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001789 return clang_getNullCursor();
1790
1791 case Decl::Function:
1792 case Decl::CXXMethod:
1793 case Decl::CXXConstructor:
1794 case Decl::CXXDestructor:
1795 case Decl::CXXConversion: {
1796 const FunctionDecl *Def = 0;
1797 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001798 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001799 return clang_getNullCursor();
1800 }
1801
1802 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001803 // Ask the variable if it has a definition.
1804 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1805 return MakeCXCursor(Def, CXXUnit);
1806 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001807 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001808
Douglas Gregorb6998662010-01-19 19:34:47 +00001809 case Decl::FunctionTemplate: {
1810 const FunctionDecl *Def = 0;
1811 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001812 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001813 return clang_getNullCursor();
1814 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001815
Douglas Gregorb6998662010-01-19 19:34:47 +00001816 case Decl::ClassTemplate: {
1817 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001818 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001819 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001820 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001821 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001822 return clang_getNullCursor();
1823 }
1824
1825 case Decl::Using: {
1826 UsingDecl *Using = cast<UsingDecl>(D);
1827 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001828 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1829 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001830 S != SEnd; ++S) {
1831 if (Def != clang_getNullCursor()) {
1832 // FIXME: We have no way to return multiple results.
1833 return clang_getNullCursor();
1834 }
1835
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001836 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001837 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001838 }
1839
1840 return Def;
1841 }
1842
1843 case Decl::UsingShadow:
1844 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001845 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001846 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001847
1848 case Decl::ObjCMethod: {
1849 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1850 if (Method->isThisDeclarationADefinition())
1851 return C;
1852
1853 // Dig out the method definition in the associated
1854 // @implementation, if we have it.
1855 // FIXME: The ASTs should make finding the definition easier.
1856 if (ObjCInterfaceDecl *Class
1857 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1858 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1859 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1860 Method->isInstanceMethod()))
1861 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001862 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001863
1864 return clang_getNullCursor();
1865 }
1866
1867 case Decl::ObjCCategory:
1868 if (ObjCCategoryImplDecl *Impl
1869 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001870 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001871 return clang_getNullCursor();
1872
1873 case Decl::ObjCProtocol:
1874 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1875 return C;
1876 return clang_getNullCursor();
1877
1878 case Decl::ObjCInterface:
1879 // There are two notions of a "definition" for an Objective-C
1880 // class: the interface and its implementation. When we resolved a
1881 // reference to an Objective-C class, produce the @interface as
1882 // the definition; when we were provided with the interface,
1883 // produce the @implementation as the definition.
1884 if (WasReference) {
1885 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1886 return C;
1887 } else if (ObjCImplementationDecl *Impl
1888 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001889 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001890 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001891
Douglas Gregorb6998662010-01-19 19:34:47 +00001892 case Decl::ObjCProperty:
1893 // FIXME: We don't really know where to find the
1894 // ObjCPropertyImplDecls that implement this property.
1895 return clang_getNullCursor();
1896
1897 case Decl::ObjCCompatibleAlias:
1898 if (ObjCInterfaceDecl *Class
1899 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1900 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001901 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001902
Douglas Gregorb6998662010-01-19 19:34:47 +00001903 return clang_getNullCursor();
1904
1905 case Decl::ObjCForwardProtocol: {
1906 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1907 if (Forward->protocol_size() == 1)
1908 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001909 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001910 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001911
1912 // FIXME: Cannot return multiple definitions.
1913 return clang_getNullCursor();
1914 }
1915
1916 case Decl::ObjCClass: {
1917 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1918 if (Class->size() == 1) {
1919 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1920 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001921 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001922 return clang_getNullCursor();
1923 }
1924
1925 // FIXME: Cannot return multiple definitions.
1926 return clang_getNullCursor();
1927 }
1928
1929 case Decl::Friend:
1930 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001931 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001932 return clang_getNullCursor();
1933
1934 case Decl::FriendTemplate:
1935 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001936 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001937 return clang_getNullCursor();
1938 }
1939
1940 return clang_getNullCursor();
1941}
1942
1943unsigned clang_isCursorDefinition(CXCursor C) {
1944 if (!clang_isDeclaration(C.kind))
1945 return 0;
1946
1947 return clang_getCursorDefinition(C) == C;
1948}
1949
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001950void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001951 const char **startBuf,
1952 const char **endBuf,
1953 unsigned *startLine,
1954 unsigned *startColumn,
1955 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001956 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001957 assert(getCursorDecl(C) && "CXCursor has null decl");
1958 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001959 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1960 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001961
Steve Naroff4ade6d62009-09-23 17:52:52 +00001962 SourceManager &SM = FD->getASTContext().getSourceManager();
1963 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1964 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1965 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1966 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1967 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1968 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1969}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001970
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001971void clang_enableStackTraces(void) {
1972 llvm::sys::PrintStackTraceOnErrorSignal();
1973}
1974
Ted Kremenekfb480492010-01-13 21:46:36 +00001975} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001976
Ted Kremenekfb480492010-01-13 21:46:36 +00001977//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001978// Token-based Operations.
1979//===----------------------------------------------------------------------===//
1980
1981/* CXToken layout:
1982 * int_data[0]: a CXTokenKind
1983 * int_data[1]: starting token location
1984 * int_data[2]: token length
1985 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001986 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001987 * otherwise unused.
1988 */
1989extern "C" {
1990
1991CXTokenKind clang_getTokenKind(CXToken CXTok) {
1992 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1993}
1994
1995CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1996 switch (clang_getTokenKind(CXTok)) {
1997 case CXToken_Identifier:
1998 case CXToken_Keyword:
1999 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002000 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2001 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002002
2003 case CXToken_Literal: {
2004 // We have stashed the starting pointer in the ptr_data field. Use it.
2005 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002006 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002007 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002008
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002009 case CXToken_Punctuation:
2010 case CXToken_Comment:
2011 break;
2012 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002013
2014 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002015 // deconstructing the source location.
2016 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2017 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002018 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002019
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002020 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2021 std::pair<FileID, unsigned> LocInfo
2022 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
2023 std::pair<const char *,const char *> Buffer
2024 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
2025
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002026 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
2027 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002028}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002029
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002030CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2031 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2032 if (!CXXUnit)
2033 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002034
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002035 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2036 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2037}
2038
2039CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2040 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002041 if (!CXXUnit)
2042 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002043
2044 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002045 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2046}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002047
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002048void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2049 CXToken **Tokens, unsigned *NumTokens) {
2050 if (Tokens)
2051 *Tokens = 0;
2052 if (NumTokens)
2053 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002054
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002055 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2056 if (!CXXUnit || !Tokens || !NumTokens)
2057 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002058
Douglas Gregorbdf60622010-03-05 21:16:25 +00002059 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2060
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002061 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002062 if (R.isInvalid())
2063 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002064
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002065 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2066 std::pair<FileID, unsigned> BeginLocInfo
2067 = SourceMgr.getDecomposedLoc(R.getBegin());
2068 std::pair<FileID, unsigned> EndLocInfo
2069 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002070
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002071 // Cannot tokenize across files.
2072 if (BeginLocInfo.first != EndLocInfo.first)
2073 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002074
2075 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002076 std::pair<const char *,const char *> Buffer
2077 = SourceMgr.getBufferData(BeginLocInfo.first);
2078 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2079 CXXUnit->getASTContext().getLangOptions(),
2080 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2081 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002082
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002083 // Lex tokens until we hit the end of the range.
2084 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2085 llvm::SmallVector<CXToken, 32> CXTokens;
2086 Token Tok;
2087 do {
2088 // Lex the next token
2089 Lex.LexFromRawLexer(Tok);
2090 if (Tok.is(tok::eof))
2091 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002092
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002093 // Initialize the CXToken.
2094 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002095
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002096 // - Common fields
2097 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2098 CXTok.int_data[2] = Tok.getLength();
2099 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002100
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002101 // - Kind-specific fields
2102 if (Tok.isLiteral()) {
2103 CXTok.int_data[0] = CXToken_Literal;
2104 CXTok.ptr_data = (void *)Tok.getLiteralData();
2105 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002106 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002107 std::pair<FileID, unsigned> LocInfo
2108 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002109 const char *StartPos
2110 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002111 LocInfo.second;
2112 IdentifierInfo *II
2113 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2114 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2115 CXToken_Identifier
2116 : CXToken_Keyword;
2117 CXTok.ptr_data = II;
2118 } else if (Tok.is(tok::comment)) {
2119 CXTok.int_data[0] = CXToken_Comment;
2120 CXTok.ptr_data = 0;
2121 } else {
2122 CXTok.int_data[0] = CXToken_Punctuation;
2123 CXTok.ptr_data = 0;
2124 }
2125 CXTokens.push_back(CXTok);
2126 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002127
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002128 if (CXTokens.empty())
2129 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002130
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002131 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2132 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2133 *NumTokens = CXTokens.size();
2134}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002135
2136typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2137
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002138enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2139 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002140 CXClientData client_data) {
2141 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2142
2143 // We only annotate the locations of declarations, simple
2144 // references, and expressions which directly reference something.
2145 CXCursorKind Kind = clang_getCursorKind(cursor);
2146 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2147 // Okay: We can annotate the location of this declaration with the
2148 // declaration or reference
2149 } else if (clang_isExpression(cursor.kind)) {
2150 if (Kind != CXCursor_DeclRefExpr &&
2151 Kind != CXCursor_MemberRefExpr &&
2152 Kind != CXCursor_ObjCMessageExpr)
2153 return CXChildVisit_Recurse;
2154
2155 CXCursor Referenced = clang_getCursorReferenced(cursor);
2156 if (Referenced == cursor || Referenced == clang_getNullCursor())
2157 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002158
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002159 // Okay: we can annotate the location of this expression
2160 } else {
2161 // Nothing to annotate
2162 return CXChildVisit_Recurse;
2163 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002164
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002165 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2166 (*Data)[Loc.int_data] = cursor;
2167 return CXChildVisit_Recurse;
2168}
2169
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002170void clang_annotateTokens(CXTranslationUnit TU,
2171 CXToken *Tokens, unsigned NumTokens,
2172 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002173 if (NumTokens == 0)
2174 return;
2175
2176 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002177 for (unsigned I = 0; I != NumTokens; ++I)
2178 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002179
2180 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2181 if (!CXXUnit || !Tokens)
2182 return;
2183
Douglas Gregorbdf60622010-03-05 21:16:25 +00002184 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2185
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002186 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002187 SourceRange RegionOfInterest;
2188 RegionOfInterest.setBegin(
2189 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2190 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002191 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002192 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002193 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002194 // FIXME: Would be great to have a "hint" cursor, then walk from that
2195 // hint cursor upward until we find a cursor whose source range encloses
2196 // the region of interest, rather than starting from the translation unit.
2197 AnnotateTokensData Annotated;
2198 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002199 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002200 Decl::MaxPCHLevel, RegionOfInterest);
2201 AnnotateVis.VisitChildren(Parent);
2202
2203 for (unsigned I = 0; I != NumTokens; ++I) {
2204 // Determine whether we saw a cursor at this token's location.
2205 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2206 if (Pos == Annotated.end())
2207 continue;
2208
2209 Cursors[I] = Pos->second;
2210 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002211}
2212
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002213void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002214 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002215 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002216}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002217
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002218} // end: extern "C"
2219
2220//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002221// Operations for querying linkage of a cursor.
2222//===----------------------------------------------------------------------===//
2223
2224extern "C" {
2225CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
2226 Decl *D = cxcursor::getCursorDecl(cursor);
2227 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2228 switch (ND->getLinkage()) {
2229 case NoLinkage: return CXLinkage_NoLinkage;
2230 case InternalLinkage: return CXLinkage_Internal;
2231 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2232 case ExternalLinkage: return CXLinkage_External;
2233 };
2234
2235 return CXLinkage_Invalid;
2236}
2237} // end: extern "C"
2238
2239//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002240// CXString Operations.
2241//===----------------------------------------------------------------------===//
2242
2243extern "C" {
2244const char *clang_getCString(CXString string) {
2245 return string.Spelling;
2246}
2247
2248void clang_disposeString(CXString string) {
2249 if (string.MustFreeString && string.Spelling)
2250 free((void*)string.Spelling);
2251}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002252
Ted Kremenekfb480492010-01-13 21:46:36 +00002253} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002254
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002255namespace clang { namespace cxstring {
2256CXString createCXString(const char *String, bool DupString){
2257 CXString Str;
2258 if (DupString) {
2259 Str.Spelling = strdup(String);
2260 Str.MustFreeString = 1;
2261 } else {
2262 Str.Spelling = String;
2263 Str.MustFreeString = 0;
2264 }
2265 return Str;
2266}
2267
2268CXString createCXString(llvm::StringRef String, bool DupString) {
2269 CXString Result;
2270 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2271 char *Spelling = (char *)malloc(String.size() + 1);
2272 memmove(Spelling, String.data(), String.size());
2273 Spelling[String.size()] = 0;
2274 Result.Spelling = Spelling;
2275 Result.MustFreeString = 1;
2276 } else {
2277 Result.Spelling = String.data();
2278 Result.MustFreeString = 0;
2279 }
2280 return Result;
2281}
2282}}
2283
Ted Kremenek04bb7162010-01-22 22:44:15 +00002284//===----------------------------------------------------------------------===//
2285// Misc. utility functions.
2286//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002287
Ted Kremenek04bb7162010-01-22 22:44:15 +00002288extern "C" {
2289
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002290CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002291 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002292}
2293
2294} // end: extern "C"