blob: 4ae2f806062f4d89ea0ebf8a077f33f09cabc72a [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) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000535 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
536 if (Visit(TSInfo->getTypeLoc()))
537 return true;
538
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000539 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000540 PEnd = ND->param_end();
541 P != PEnd; ++P) {
542 if (Visit(MakeCXCursor(*P, TU)))
543 return true;
544 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000545
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000546 if (ND->isThisDeclarationADefinition() &&
547 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
548 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000549
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000550 return false;
551}
552
Douglas Gregora59e3902010-01-21 23:27:09 +0000553bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
554 return VisitDeclContext(D);
555}
556
Douglas Gregorb1373d02010-01-20 20:59:29 +0000557bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000558 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
559 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000560 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000561
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000562 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
563 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
564 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000565 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000566 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000567
Douglas Gregora59e3902010-01-21 23:27:09 +0000568 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000569}
570
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000571bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
572 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
573 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
574 E = PID->protocol_end(); I != E; ++I, ++PL)
575 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
576 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000577
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000578 return VisitObjCContainerDecl(PID);
579}
580
Douglas Gregorb1373d02010-01-20 20:59:29 +0000581bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000582 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000583 if (D->getSuperClass() &&
584 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000585 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000586 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000587 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000588
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000589 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
590 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
591 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000592 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000593 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000594
Douglas Gregora59e3902010-01-21 23:27:09 +0000595 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000596}
597
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000598bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
599 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000600}
601
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000602bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000603 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000604 D->getLocation(), TU)))
605 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000606
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000607 return VisitObjCImplDecl(D);
608}
609
610bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
611#if 0
612 // Issue callbacks for super class.
613 // FIXME: No source location information!
614 if (D->getSuperClass() &&
615 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000616 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000617 TU)))
618 return true;
619#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000620
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000621 return VisitObjCImplDecl(D);
622}
623
624bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
625 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
626 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
627 E = D->protocol_end();
628 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000629 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000630 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000631
632 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000633}
634
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000635bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
636 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
637 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
638 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000639
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000640 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000641}
642
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000643bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
644 ASTContext &Context = TU->getASTContext();
645
646 // Some builtin types (such as Objective-C's "id", "sel", and
647 // "Class") have associated declarations. Create cursors for those.
648 QualType VisitType;
649 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000650 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000651 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000652 case BuiltinType::Char_U:
653 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000654 case BuiltinType::Char16:
655 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000656 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000657 case BuiltinType::UInt:
658 case BuiltinType::ULong:
659 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000660 case BuiltinType::UInt128:
661 case BuiltinType::Char_S:
662 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000663 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000664 case BuiltinType::Short:
665 case BuiltinType::Int:
666 case BuiltinType::Long:
667 case BuiltinType::LongLong:
668 case BuiltinType::Int128:
669 case BuiltinType::Float:
670 case BuiltinType::Double:
671 case BuiltinType::LongDouble:
672 case BuiltinType::NullPtr:
673 case BuiltinType::Overload:
674 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000675 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000676
677 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000678 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000679
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000680 case BuiltinType::ObjCId:
681 VisitType = Context.getObjCIdType();
682 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000683
684 case BuiltinType::ObjCClass:
685 VisitType = Context.getObjCClassType();
686 break;
687
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000688 case BuiltinType::ObjCSel:
689 VisitType = Context.getObjCSelType();
690 break;
691 }
692
693 if (!VisitType.isNull()) {
694 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000695 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000696 TU));
697 }
698
699 return false;
700}
701
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000702bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
703 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
704}
705
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000706bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
707 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
708}
709
710bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
711 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
712}
713
714bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
715 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
716 return true;
717
718 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
719 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
720 TU)))
721 return true;
722 }
723
724 return false;
725}
726
727bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
728 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
729 return true;
730
731 if (TL.hasProtocolsAsWritten()) {
732 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000733 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000734 TL.getProtocolLoc(I),
735 TU)))
736 return true;
737 }
738 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000739
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000740 return false;
741}
742
743bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
744 return Visit(TL.getPointeeLoc());
745}
746
747bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
748 return Visit(TL.getPointeeLoc());
749}
750
751bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
752 return Visit(TL.getPointeeLoc());
753}
754
755bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000756 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000757}
758
759bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000760 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000761}
762
763bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
764 if (Visit(TL.getResultLoc()))
765 return true;
766
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000767 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
768 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
769 return true;
770
771 return false;
772}
773
774bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
775 if (Visit(TL.getElementLoc()))
776 return true;
777
778 if (Expr *Size = TL.getSizeExpr())
779 return Visit(MakeCXCursor(Size, StmtParent, TU));
780
781 return false;
782}
783
Douglas Gregor2332c112010-01-21 20:48:56 +0000784bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
785 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
786}
787
788bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
789 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
790 return Visit(TSInfo->getTypeLoc());
791
792 return false;
793}
794
Douglas Gregora59e3902010-01-21 23:27:09 +0000795bool CursorVisitor::VisitStmt(Stmt *S) {
796 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
797 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000798 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000799 return true;
800 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000801
Douglas Gregora59e3902010-01-21 23:27:09 +0000802 return false;
803}
804
805bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
806 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
807 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000808 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000809 return true;
810 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000811
Douglas Gregora59e3902010-01-21 23:27:09 +0000812 return false;
813}
814
Douglas Gregorf5bab412010-01-22 01:00:11 +0000815bool CursorVisitor::VisitIfStmt(IfStmt *S) {
816 if (VarDecl *Var = S->getConditionVariable()) {
817 if (Visit(MakeCXCursor(Var, TU)))
818 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000819 }
820
Douglas Gregor263b47b2010-01-25 16:12:32 +0000821 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
822 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000823 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
824 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000825 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
826 return true;
827
828 return false;
829}
830
831bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
832 if (VarDecl *Var = S->getConditionVariable()) {
833 if (Visit(MakeCXCursor(Var, TU)))
834 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000835 }
836
Douglas Gregor263b47b2010-01-25 16:12:32 +0000837 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
838 return true;
839 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
840 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000841
Douglas Gregor263b47b2010-01-25 16:12:32 +0000842 return false;
843}
844
845bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
846 if (VarDecl *Var = S->getConditionVariable()) {
847 if (Visit(MakeCXCursor(Var, TU)))
848 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000849 }
850
Douglas Gregor263b47b2010-01-25 16:12:32 +0000851 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
852 return true;
853 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000854 return true;
855
Douglas Gregor263b47b2010-01-25 16:12:32 +0000856 return false;
857}
858
859bool CursorVisitor::VisitForStmt(ForStmt *S) {
860 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
861 return true;
862 if (VarDecl *Var = S->getConditionVariable()) {
863 if (Visit(MakeCXCursor(Var, TU)))
864 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000865 }
866
Douglas Gregor263b47b2010-01-25 16:12:32 +0000867 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
868 return true;
869 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
870 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000871 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
872 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000873
Douglas Gregorf5bab412010-01-22 01:00:11 +0000874 return false;
875}
876
Douglas Gregor336fd812010-01-23 00:40:08 +0000877bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
878 if (E->isArgumentType()) {
879 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
880 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000881
Douglas Gregor336fd812010-01-23 00:40:08 +0000882 return false;
883 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000884
Douglas Gregor336fd812010-01-23 00:40:08 +0000885 return VisitExpr(E);
886}
887
888bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
889 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
890 if (Visit(TSInfo->getTypeLoc()))
891 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000892
Douglas Gregor336fd812010-01-23 00:40:08 +0000893 return VisitCastExpr(E);
894}
895
896bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
897 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
898 if (Visit(TSInfo->getTypeLoc()))
899 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000900
Douglas Gregor336fd812010-01-23 00:40:08 +0000901 return VisitExpr(E);
902}
903
Ted Kremenek09dfa372010-02-18 05:46:33 +0000904bool CursorVisitor::VisitAttributes(Decl *D) {
905 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
906 if (Visit(MakeCXCursor(A, D, TU)))
907 return true;
908
909 return false;
910}
911
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000912extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000913CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
914 int displayDiagnostics) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000915 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000916 if (excludeDeclarationsFromPCH)
917 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000918 if (displayDiagnostics)
919 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000920 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000921}
922
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000923void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000924 if (CIdx)
925 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000926}
927
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000928void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000929 if (CIdx) {
930 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
931 CXXIdx->setUseExternalASTGeneration(value);
932 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000933}
934
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000935CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +0000936 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000937 if (!CIdx)
938 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000939
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000940 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000941
Douglas Gregor5352ac02010-01-28 00:27:43 +0000942 // Configure the diagnostics.
943 DiagnosticOptions DiagOpts;
944 llvm::OwningPtr<Diagnostic> Diags;
945 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000946 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000947 CXXIdx->getOnlyLocalDecls(),
948 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000949}
950
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000951CXTranslationUnit
952clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
953 const char *source_filename,
954 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000955 const char **command_line_args,
956 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000957 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000958 if (!CIdx)
959 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000960
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000961 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
962
Douglas Gregor5352ac02010-01-28 00:27:43 +0000963 // Configure the diagnostics.
964 DiagnosticOptions DiagOpts;
965 llvm::OwningPtr<Diagnostic> Diags;
966 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000967
Douglas Gregor4db64a42010-01-23 00:14:00 +0000968 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
969 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000970 const llvm::MemoryBuffer *Buffer
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000971 = llvm::MemoryBuffer::getMemBufferCopy(unsaved_files[I].Contents,
Douglas Gregor313e26c2010-02-18 23:35:40 +0000972 unsaved_files[I].Contents + unsaved_files[I].Length,
973 unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000974 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
975 Buffer));
976 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000977
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000978 if (!CXXIdx->getUseExternalASTGeneration()) {
979 llvm::SmallVector<const char *, 16> Args;
980
981 // The 'source_filename' argument is optional. If the caller does not
982 // specify it then it is assumed that the source file is specified
983 // in the actual argument list.
984 if (source_filename)
985 Args.push_back(source_filename);
986 Args.insert(Args.end(), command_line_args,
987 command_line_args + num_command_line_args);
988
Douglas Gregor5352ac02010-01-28 00:27:43 +0000989 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000990
Ted Kremenek29b72842010-01-07 22:49:05 +0000991#ifdef USE_CRASHTRACER
992 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000993#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000994
Daniel Dunbar94220972009-12-05 02:17:18 +0000995 llvm::OwningPtr<ASTUnit> Unit(
996 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000997 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000998 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000999 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001000 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001001 RemappedFiles.size(),
1002 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001003
Daniel Dunbar94220972009-12-05 02:17:18 +00001004 // FIXME: Until we have broader testing, just drop the entire AST if we
1005 // encountered an error.
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001006 if (NumErrors != Diags->getNumErrors()) {
Ted Kremenek34f6a322010-03-05 22:43:29 +00001007 // Make sure to check that 'Unit' is non-NULL.
1008 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001009 for (ASTUnit::diag_iterator D = Unit->diag_begin(),
1010 DEnd = Unit->diag_end();
1011 D != DEnd; ++D) {
1012 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001013 CXString Msg = clang_formatDiagnostic(&Diag,
1014 clang_defaultDiagnosticDisplayOptions());
1015 fprintf(stderr, "%s\n", clang_getCString(Msg));
1016 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001017 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001018#ifdef LLVM_ON_WIN32
1019 // On Windows, force a flush, since there may be multiple copies of
1020 // stderr and stdout in the file system, all with different buffers
1021 // but writing to the same device.
1022 fflush(stderr);
1023#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001024 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001025 return 0;
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001026 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001027
1028 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001029 }
1030
Ted Kremenek139ba862009-10-22 00:03:57 +00001031 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001032 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001033
Ted Kremenek139ba862009-10-22 00:03:57 +00001034 // First add the complete path to the 'clang' executable.
1035 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001036 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001037
Ted Kremenek139ba862009-10-22 00:03:57 +00001038 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001039 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001040
Ted Kremenek139ba862009-10-22 00:03:57 +00001041 // The 'source_filename' argument is optional. If the caller does not
1042 // specify it then it is assumed that the source file is specified
1043 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001044 if (source_filename)
1045 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001046
Steve Naroff37b5ac22009-10-15 20:50:09 +00001047 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001048 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001049 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001050 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001051
Douglas Gregor4db64a42010-01-23 00:14:00 +00001052 // Remap any unsaved files to temporary files.
1053 std::vector<llvm::sys::Path> TemporaryFiles;
1054 std::vector<std::string> RemapArgs;
1055 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1056 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001057
Douglas Gregor4db64a42010-01-23 00:14:00 +00001058 // The pointers into the elements of RemapArgs are stable because we
1059 // won't be adding anything to RemapArgs after this point.
1060 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1061 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001062
Ted Kremenek139ba862009-10-22 00:03:57 +00001063 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1064 for (int i = 0; i < num_command_line_args; ++i)
1065 if (const char *arg = command_line_args[i]) {
1066 if (strcmp(arg, "-o") == 0) {
1067 ++i; // Also skip the matching argument.
1068 continue;
1069 }
1070 if (strcmp(arg, "-emit-ast") == 0 ||
1071 strcmp(arg, "-c") == 0 ||
1072 strcmp(arg, "-fsyntax-only") == 0) {
1073 continue;
1074 }
1075
1076 // Keep the argument.
1077 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001078 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001079
Douglas Gregord93256e2010-01-28 06:00:51 +00001080 // Generate a temporary name for the diagnostics file.
1081 char tmpFileResults[L_tmpnam];
1082 char *tmpResultsFileName = tmpnam(tmpFileResults);
1083 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1084 TemporaryFiles.push_back(DiagnosticsFile);
1085 argv.push_back("-fdiagnostics-binary");
1086
Ted Kremenek139ba862009-10-22 00:03:57 +00001087 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001088 argv.push_back(NULL);
1089
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001090 // Invoke 'clang'.
1091 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1092 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001093 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001094 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1095 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001096 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001097 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001098 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001099
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001100 if (!ErrMsg.empty()) {
1101 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001102 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001103 I != E; ++I) {
1104 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001105 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001106 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001107 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001108
Daniel Dunbar32141c82010-02-23 20:23:45 +00001109 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001110 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001111
Douglas Gregor5352ac02010-01-28 00:27:43 +00001112 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001113 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001114 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001115 RemappedFiles.size(),
1116 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001117 if (ATU) {
1118 LoadSerializedDiagnostics(DiagnosticsFile,
1119 num_unsaved_files, unsaved_files,
1120 ATU->getFileManager(),
1121 ATU->getSourceManager(),
1122 ATU->getDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001123 } else if (CXXIdx->getDisplayDiagnostics()) {
1124 // We failed to load the ASTUnit, but we can still deserialize the
1125 // diagnostics and emit them.
1126 FileManager FileMgr;
1127 SourceManager SourceMgr;
1128 // FIXME: Faked LangOpts!
1129 LangOptions LangOpts;
1130 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1131 LoadSerializedDiagnostics(DiagnosticsFile,
1132 num_unsaved_files, unsaved_files,
1133 FileMgr, SourceMgr, Diags);
1134 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1135 DEnd = Diags.end();
1136 D != DEnd; ++D) {
1137 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001138 CXString Msg = clang_formatDiagnostic(&Diag,
1139 clang_defaultDiagnosticDisplayOptions());
1140 fprintf(stderr, "%s\n", clang_getCString(Msg));
1141 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001142 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001143
1144#ifdef LLVM_ON_WIN32
1145 // On Windows, force a flush, since there may be multiple copies of
1146 // stderr and stdout in the file system, all with different buffers
1147 // but writing to the same device.
1148 fflush(stderr);
1149#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001150 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001151
Douglas Gregor313e26c2010-02-18 23:35:40 +00001152 if (ATU) {
1153 // Make the translation unit responsible for destroying all temporary files.
1154 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1155 ATU->addTemporaryFile(TemporaryFiles[i]);
1156 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1157 } else {
1158 // Destroy all of the temporary files now; they can't be referenced any
1159 // longer.
1160 llvm::sys::Path(astTmpFile).eraseFromDisk();
1161 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1162 TemporaryFiles[i].eraseFromDisk();
1163 }
1164
Steve Naroffe19944c2009-10-15 22:23:48 +00001165 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001166}
1167
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001168void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001169 if (CTUnit)
1170 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001171}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001172
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001173CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001174 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001175 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001176
Steve Naroff77accc12009-09-03 18:19:54 +00001177 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001178 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001179}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001180
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001181CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001182 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001183 return Result;
1184}
1185
Ted Kremenekfb480492010-01-13 21:46:36 +00001186} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001187
Ted Kremenekfb480492010-01-13 21:46:36 +00001188//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001189// CXSourceLocation and CXSourceRange Operations.
1190//===----------------------------------------------------------------------===//
1191
Douglas Gregorb9790342010-01-22 21:44:22 +00001192extern "C" {
1193CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001194 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001195 return Result;
1196}
1197
1198unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001199 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1200 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1201 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001202}
1203
1204CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1205 CXFile file,
1206 unsigned line,
1207 unsigned column) {
1208 if (!tu)
1209 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001210
Douglas Gregorb9790342010-01-22 21:44:22 +00001211 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1212 SourceLocation SLoc
1213 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001214 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001215 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001216
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001217 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001218}
1219
Douglas Gregor5352ac02010-01-28 00:27:43 +00001220CXSourceRange clang_getNullRange() {
1221 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1222 return Result;
1223}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001224
Douglas Gregor5352ac02010-01-28 00:27:43 +00001225CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1226 if (begin.ptr_data[0] != end.ptr_data[0] ||
1227 begin.ptr_data[1] != end.ptr_data[1])
1228 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001229
1230 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001231 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001232 return Result;
1233}
1234
Douglas Gregor46766dc2010-01-26 19:19:08 +00001235void clang_getInstantiationLocation(CXSourceLocation location,
1236 CXFile *file,
1237 unsigned *line,
1238 unsigned *column,
1239 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001240 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1241
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001242 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001243 if (file)
1244 *file = 0;
1245 if (line)
1246 *line = 0;
1247 if (column)
1248 *column = 0;
1249 if (offset)
1250 *offset = 0;
1251 return;
1252 }
1253
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001254 const SourceManager &SM =
1255 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001256 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001257
1258 if (file)
1259 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1260 if (line)
1261 *line = SM.getInstantiationLineNumber(InstLoc);
1262 if (column)
1263 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001264 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001265 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001266}
1267
Douglas Gregor1db19de2010-01-19 21:36:55 +00001268CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001269 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001270 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001271 return Result;
1272}
1273
1274CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001275 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001276 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001277 return Result;
1278}
1279
Douglas Gregorb9790342010-01-22 21:44:22 +00001280} // end: extern "C"
1281
Douglas Gregor1db19de2010-01-19 21:36:55 +00001282//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001283// CXFile Operations.
1284//===----------------------------------------------------------------------===//
1285
1286extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001287CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001288 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001289 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001290
Steve Naroff88145032009-10-27 14:35:18 +00001291 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001292 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001293}
1294
1295time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001296 if (!SFile)
1297 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001298
Steve Naroff88145032009-10-27 14:35:18 +00001299 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1300 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001301}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001302
Douglas Gregorb9790342010-01-22 21:44:22 +00001303CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1304 if (!tu)
1305 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001306
Douglas Gregorb9790342010-01-22 21:44:22 +00001307 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001308
Douglas Gregorb9790342010-01-22 21:44:22 +00001309 FileManager &FMgr = CXXUnit->getFileManager();
1310 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1311 return const_cast<FileEntry *>(File);
1312}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001313
Ted Kremenekfb480492010-01-13 21:46:36 +00001314} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001315
Ted Kremenekfb480492010-01-13 21:46:36 +00001316//===----------------------------------------------------------------------===//
1317// CXCursor Operations.
1318//===----------------------------------------------------------------------===//
1319
Ted Kremenekfb480492010-01-13 21:46:36 +00001320static Decl *getDeclFromExpr(Stmt *E) {
1321 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1322 return RefExpr->getDecl();
1323 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1324 return ME->getMemberDecl();
1325 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1326 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001327
Ted Kremenekfb480492010-01-13 21:46:36 +00001328 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1329 return getDeclFromExpr(CE->getCallee());
1330 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1331 return getDeclFromExpr(CE->getSubExpr());
1332 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1333 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001334
Ted Kremenekfb480492010-01-13 21:46:36 +00001335 return 0;
1336}
1337
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001338static SourceLocation getLocationFromExpr(Expr *E) {
1339 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1340 return /*FIXME:*/Msg->getLeftLoc();
1341 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1342 return DRE->getLocation();
1343 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1344 return Member->getMemberLoc();
1345 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1346 return Ivar->getLocation();
1347 return E->getLocStart();
1348}
1349
Ted Kremenekfb480492010-01-13 21:46:36 +00001350extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001351
1352unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001353 CXCursorVisitor visitor,
1354 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001355 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001356
1357 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001358
Douglas Gregorb1373d02010-01-20 20:59:29 +00001359 // Set the PCHLevel to filter out unwanted decls if requested.
1360 if (CXXUnit->getOnlyLocalDecls()) {
1361 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001362
Douglas Gregorb1373d02010-01-20 20:59:29 +00001363 // If the main input was an AST, bump the level.
1364 if (CXXUnit->isMainFileAST())
1365 ++PCHLevel;
1366 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001367
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001368 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001369 return CursorVis.VisitChildren(parent);
1370}
1371
Douglas Gregor78205d42010-01-20 21:45:58 +00001372static CXString getDeclSpelling(Decl *D) {
1373 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1374 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001375 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001376
Douglas Gregor78205d42010-01-20 21:45:58 +00001377 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001378 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001379
Douglas Gregor78205d42010-01-20 21:45:58 +00001380 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1381 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1382 // and returns different names. NamedDecl returns the class name and
1383 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001384 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001385
Douglas Gregor78205d42010-01-20 21:45:58 +00001386 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001387 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001388
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001389 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001390}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001391
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001392CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001393 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001394 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001395
Steve Narofff334b4e2009-09-02 18:26:48 +00001396 if (clang_isReference(C.kind)) {
1397 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001398 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001399 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001400 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001401 }
1402 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001403 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001404 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001405 }
1406 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001407 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001408 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001409 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001410 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001411 case CXCursor_TypeRef: {
1412 TypeDecl *Type = getCursorTypeRef(C).first;
1413 assert(Type && "Missing type decl");
1414
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001415 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1416 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001417 }
1418
Daniel Dunbaracca7252009-11-30 20:42:49 +00001419 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001420 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001421 }
1422 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001423
1424 if (clang_isExpression(C.kind)) {
1425 Decl *D = getDeclFromExpr(getCursorExpr(C));
1426 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001427 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001428 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001429 }
1430
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001431 if (clang_isDeclaration(C.kind))
1432 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001433
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001434 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001435}
1436
Ted Kremeneke68fff62010-02-17 00:41:32 +00001437CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001438 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001439 case CXCursor_FunctionDecl:
1440 return createCXString("FunctionDecl");
1441 case CXCursor_TypedefDecl:
1442 return createCXString("TypedefDecl");
1443 case CXCursor_EnumDecl:
1444 return createCXString("EnumDecl");
1445 case CXCursor_EnumConstantDecl:
1446 return createCXString("EnumConstantDecl");
1447 case CXCursor_StructDecl:
1448 return createCXString("StructDecl");
1449 case CXCursor_UnionDecl:
1450 return createCXString("UnionDecl");
1451 case CXCursor_ClassDecl:
1452 return createCXString("ClassDecl");
1453 case CXCursor_FieldDecl:
1454 return createCXString("FieldDecl");
1455 case CXCursor_VarDecl:
1456 return createCXString("VarDecl");
1457 case CXCursor_ParmDecl:
1458 return createCXString("ParmDecl");
1459 case CXCursor_ObjCInterfaceDecl:
1460 return createCXString("ObjCInterfaceDecl");
1461 case CXCursor_ObjCCategoryDecl:
1462 return createCXString("ObjCCategoryDecl");
1463 case CXCursor_ObjCProtocolDecl:
1464 return createCXString("ObjCProtocolDecl");
1465 case CXCursor_ObjCPropertyDecl:
1466 return createCXString("ObjCPropertyDecl");
1467 case CXCursor_ObjCIvarDecl:
1468 return createCXString("ObjCIvarDecl");
1469 case CXCursor_ObjCInstanceMethodDecl:
1470 return createCXString("ObjCInstanceMethodDecl");
1471 case CXCursor_ObjCClassMethodDecl:
1472 return createCXString("ObjCClassMethodDecl");
1473 case CXCursor_ObjCImplementationDecl:
1474 return createCXString("ObjCImplementationDecl");
1475 case CXCursor_ObjCCategoryImplDecl:
1476 return createCXString("ObjCCategoryImplDecl");
1477 case CXCursor_UnexposedDecl:
1478 return createCXString("UnexposedDecl");
1479 case CXCursor_ObjCSuperClassRef:
1480 return createCXString("ObjCSuperClassRef");
1481 case CXCursor_ObjCProtocolRef:
1482 return createCXString("ObjCProtocolRef");
1483 case CXCursor_ObjCClassRef:
1484 return createCXString("ObjCClassRef");
1485 case CXCursor_TypeRef:
1486 return createCXString("TypeRef");
1487 case CXCursor_UnexposedExpr:
1488 return createCXString("UnexposedExpr");
1489 case CXCursor_DeclRefExpr:
1490 return createCXString("DeclRefExpr");
1491 case CXCursor_MemberRefExpr:
1492 return createCXString("MemberRefExpr");
1493 case CXCursor_CallExpr:
1494 return createCXString("CallExpr");
1495 case CXCursor_ObjCMessageExpr:
1496 return createCXString("ObjCMessageExpr");
1497 case CXCursor_UnexposedStmt:
1498 return createCXString("UnexposedStmt");
1499 case CXCursor_InvalidFile:
1500 return createCXString("InvalidFile");
1501 case CXCursor_NoDeclFound:
1502 return createCXString("NoDeclFound");
1503 case CXCursor_NotImplemented:
1504 return createCXString("NotImplemented");
1505 case CXCursor_TranslationUnit:
1506 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001507 case CXCursor_UnexposedAttr:
1508 return createCXString("UnexposedAttr");
1509 case CXCursor_IBActionAttr:
1510 return createCXString("attribute(ibaction)");
1511 case CXCursor_IBOutletAttr:
1512 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001513 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001514
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001515 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001516 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001517}
Steve Naroff89922f82009-08-31 00:59:03 +00001518
Ted Kremeneke68fff62010-02-17 00:41:32 +00001519enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1520 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001521 CXClientData client_data) {
1522 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1523 *BestCursor = cursor;
1524 return CXChildVisit_Recurse;
1525}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001526
Douglas Gregorb9790342010-01-22 21:44:22 +00001527CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1528 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001529 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001530
Douglas Gregorb9790342010-01-22 21:44:22 +00001531 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1532
Douglas Gregorbdf60622010-03-05 21:16:25 +00001533 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1534
Ted Kremeneka297de22010-01-25 22:34:44 +00001535 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001536 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1537 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001538 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001539
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001540 // FIXME: Would be great to have a "hint" cursor, then walk from that
1541 // hint cursor upward until we find a cursor whose source range encloses
1542 // the region of interest, rather than starting from the translation unit.
1543 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001544 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001545 Decl::MaxPCHLevel, RegionOfInterest);
1546 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001547 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001548 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001549}
1550
Ted Kremenek73885552009-11-17 19:28:59 +00001551CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001552 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001553}
1554
1555unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001556 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001557}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001558
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001559unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001560 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1561}
1562
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001563unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001564 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1565}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001566
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001567unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001568 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1569}
1570
Douglas Gregor97b98722010-01-19 23:20:36 +00001571unsigned clang_isExpression(enum CXCursorKind K) {
1572 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1573}
1574
1575unsigned clang_isStatement(enum CXCursorKind K) {
1576 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1577}
1578
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001579unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1580 return K == CXCursor_TranslationUnit;
1581}
1582
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001583CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001584 return C.kind;
1585}
1586
Douglas Gregor98258af2010-01-18 22:46:11 +00001587CXSourceLocation clang_getCursorLocation(CXCursor C) {
1588 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001589 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001590 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001591 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1592 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001593 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001594 }
1595
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001596 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001597 std::pair<ObjCProtocolDecl *, SourceLocation> P
1598 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001599 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001600 }
1601
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001602 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001603 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1604 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001605 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001606 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001607
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001608 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001609 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001610 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001611 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001612
Douglas Gregorf46034a2010-01-18 23:41:10 +00001613 default:
1614 // FIXME: Need a way to enumerate all non-reference cases.
1615 llvm_unreachable("Missed a reference kind");
1616 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001617 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001618
1619 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001620 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001621 getLocationFromExpr(getCursorExpr(C)));
1622
Douglas Gregor5352ac02010-01-28 00:27:43 +00001623 if (!getCursorDecl(C))
1624 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001625
Douglas Gregorf46034a2010-01-18 23:41:10 +00001626 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001627 SourceLocation Loc = D->getLocation();
1628 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1629 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001630 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001631}
Douglas Gregora7bde202010-01-19 00:34:46 +00001632
1633CXSourceRange clang_getCursorExtent(CXCursor C) {
1634 if (clang_isReference(C.kind)) {
1635 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001636 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001637 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1638 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001639 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001640 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001641
1642 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001643 std::pair<ObjCProtocolDecl *, SourceLocation> P
1644 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001645 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001646 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001647
1648 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001649 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1650 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001651
Ted Kremeneka297de22010-01-25 22:34:44 +00001652 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001653 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001654
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001655 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001656 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001657 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001658 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001659
Douglas Gregora7bde202010-01-19 00:34:46 +00001660 default:
1661 // FIXME: Need a way to enumerate all non-reference cases.
1662 llvm_unreachable("Missed a reference kind");
1663 }
1664 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001665
1666 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001667 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001668 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001669
1670 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001671 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001672 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001673
Douglas Gregor5352ac02010-01-28 00:27:43 +00001674 if (!getCursorDecl(C))
1675 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001676
Douglas Gregora7bde202010-01-19 00:34:46 +00001677 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001678 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001679}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001680
1681CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001682 if (clang_isInvalid(C.kind))
1683 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001684
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001685 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001686 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001687 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001688
Douglas Gregor97b98722010-01-19 23:20:36 +00001689 if (clang_isExpression(C.kind)) {
1690 Decl *D = getDeclFromExpr(getCursorExpr(C));
1691 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001692 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001693 return clang_getNullCursor();
1694 }
1695
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001696 if (!clang_isReference(C.kind))
1697 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001698
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001699 switch (C.kind) {
1700 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001701 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001702
1703 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001704 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001705
1706 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001707 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001708
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001709 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001710 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001711
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001712 default:
1713 // We would prefer to enumerate all non-reference cursor kinds here.
1714 llvm_unreachable("Unhandled reference cursor kind");
1715 break;
1716 }
1717 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001718
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001719 return clang_getNullCursor();
1720}
1721
Douglas Gregorb6998662010-01-19 19:34:47 +00001722CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001723 if (clang_isInvalid(C.kind))
1724 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001725
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001726 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001727
Douglas Gregorb6998662010-01-19 19:34:47 +00001728 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001729 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001730 C = clang_getCursorReferenced(C);
1731 WasReference = true;
1732 }
1733
1734 if (!clang_isDeclaration(C.kind))
1735 return clang_getNullCursor();
1736
1737 Decl *D = getCursorDecl(C);
1738 if (!D)
1739 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001740
Douglas Gregorb6998662010-01-19 19:34:47 +00001741 switch (D->getKind()) {
1742 // Declaration kinds that don't really separate the notions of
1743 // declaration and definition.
1744 case Decl::Namespace:
1745 case Decl::Typedef:
1746 case Decl::TemplateTypeParm:
1747 case Decl::EnumConstant:
1748 case Decl::Field:
1749 case Decl::ObjCIvar:
1750 case Decl::ObjCAtDefsField:
1751 case Decl::ImplicitParam:
1752 case Decl::ParmVar:
1753 case Decl::NonTypeTemplateParm:
1754 case Decl::TemplateTemplateParm:
1755 case Decl::ObjCCategoryImpl:
1756 case Decl::ObjCImplementation:
1757 case Decl::LinkageSpec:
1758 case Decl::ObjCPropertyImpl:
1759 case Decl::FileScopeAsm:
1760 case Decl::StaticAssert:
1761 case Decl::Block:
1762 return C;
1763
1764 // Declaration kinds that don't make any sense here, but are
1765 // nonetheless harmless.
1766 case Decl::TranslationUnit:
1767 case Decl::Template:
1768 case Decl::ObjCContainer:
1769 break;
1770
1771 // Declaration kinds for which the definition is not resolvable.
1772 case Decl::UnresolvedUsingTypename:
1773 case Decl::UnresolvedUsingValue:
1774 break;
1775
1776 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001777 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1778 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001779
1780 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001781 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001782
1783 case Decl::Enum:
1784 case Decl::Record:
1785 case Decl::CXXRecord:
1786 case Decl::ClassTemplateSpecialization:
1787 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001788 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001789 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001790 return clang_getNullCursor();
1791
1792 case Decl::Function:
1793 case Decl::CXXMethod:
1794 case Decl::CXXConstructor:
1795 case Decl::CXXDestructor:
1796 case Decl::CXXConversion: {
1797 const FunctionDecl *Def = 0;
1798 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001799 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001800 return clang_getNullCursor();
1801 }
1802
1803 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001804 // Ask the variable if it has a definition.
1805 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1806 return MakeCXCursor(Def, CXXUnit);
1807 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001808 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001809
Douglas Gregorb6998662010-01-19 19:34:47 +00001810 case Decl::FunctionTemplate: {
1811 const FunctionDecl *Def = 0;
1812 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001813 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001814 return clang_getNullCursor();
1815 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001816
Douglas Gregorb6998662010-01-19 19:34:47 +00001817 case Decl::ClassTemplate: {
1818 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001819 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001820 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001821 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001822 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001823 return clang_getNullCursor();
1824 }
1825
1826 case Decl::Using: {
1827 UsingDecl *Using = cast<UsingDecl>(D);
1828 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001829 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1830 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001831 S != SEnd; ++S) {
1832 if (Def != clang_getNullCursor()) {
1833 // FIXME: We have no way to return multiple results.
1834 return clang_getNullCursor();
1835 }
1836
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001837 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001838 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001839 }
1840
1841 return Def;
1842 }
1843
1844 case Decl::UsingShadow:
1845 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001846 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001847 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001848
1849 case Decl::ObjCMethod: {
1850 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1851 if (Method->isThisDeclarationADefinition())
1852 return C;
1853
1854 // Dig out the method definition in the associated
1855 // @implementation, if we have it.
1856 // FIXME: The ASTs should make finding the definition easier.
1857 if (ObjCInterfaceDecl *Class
1858 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1859 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1860 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1861 Method->isInstanceMethod()))
1862 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001863 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001864
1865 return clang_getNullCursor();
1866 }
1867
1868 case Decl::ObjCCategory:
1869 if (ObjCCategoryImplDecl *Impl
1870 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001871 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001872 return clang_getNullCursor();
1873
1874 case Decl::ObjCProtocol:
1875 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1876 return C;
1877 return clang_getNullCursor();
1878
1879 case Decl::ObjCInterface:
1880 // There are two notions of a "definition" for an Objective-C
1881 // class: the interface and its implementation. When we resolved a
1882 // reference to an Objective-C class, produce the @interface as
1883 // the definition; when we were provided with the interface,
1884 // produce the @implementation as the definition.
1885 if (WasReference) {
1886 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1887 return C;
1888 } else if (ObjCImplementationDecl *Impl
1889 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001890 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001891 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001892
Douglas Gregorb6998662010-01-19 19:34:47 +00001893 case Decl::ObjCProperty:
1894 // FIXME: We don't really know where to find the
1895 // ObjCPropertyImplDecls that implement this property.
1896 return clang_getNullCursor();
1897
1898 case Decl::ObjCCompatibleAlias:
1899 if (ObjCInterfaceDecl *Class
1900 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1901 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001902 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001903
Douglas Gregorb6998662010-01-19 19:34:47 +00001904 return clang_getNullCursor();
1905
1906 case Decl::ObjCForwardProtocol: {
1907 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1908 if (Forward->protocol_size() == 1)
1909 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001910 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001911 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001912
1913 // FIXME: Cannot return multiple definitions.
1914 return clang_getNullCursor();
1915 }
1916
1917 case Decl::ObjCClass: {
1918 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1919 if (Class->size() == 1) {
1920 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1921 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001922 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001923 return clang_getNullCursor();
1924 }
1925
1926 // FIXME: Cannot return multiple definitions.
1927 return clang_getNullCursor();
1928 }
1929
1930 case Decl::Friend:
1931 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001932 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001933 return clang_getNullCursor();
1934
1935 case Decl::FriendTemplate:
1936 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001937 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001938 return clang_getNullCursor();
1939 }
1940
1941 return clang_getNullCursor();
1942}
1943
1944unsigned clang_isCursorDefinition(CXCursor C) {
1945 if (!clang_isDeclaration(C.kind))
1946 return 0;
1947
1948 return clang_getCursorDefinition(C) == C;
1949}
1950
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001951void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001952 const char **startBuf,
1953 const char **endBuf,
1954 unsigned *startLine,
1955 unsigned *startColumn,
1956 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001957 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001958 assert(getCursorDecl(C) && "CXCursor has null decl");
1959 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001960 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1961 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001962
Steve Naroff4ade6d62009-09-23 17:52:52 +00001963 SourceManager &SM = FD->getASTContext().getSourceManager();
1964 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1965 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1966 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1967 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1968 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1969 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1970}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001971
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001972void clang_enableStackTraces(void) {
1973 llvm::sys::PrintStackTraceOnErrorSignal();
1974}
1975
Ted Kremenekfb480492010-01-13 21:46:36 +00001976} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001977
Ted Kremenekfb480492010-01-13 21:46:36 +00001978//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001979// Token-based Operations.
1980//===----------------------------------------------------------------------===//
1981
1982/* CXToken layout:
1983 * int_data[0]: a CXTokenKind
1984 * int_data[1]: starting token location
1985 * int_data[2]: token length
1986 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001987 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001988 * otherwise unused.
1989 */
1990extern "C" {
1991
1992CXTokenKind clang_getTokenKind(CXToken CXTok) {
1993 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1994}
1995
1996CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1997 switch (clang_getTokenKind(CXTok)) {
1998 case CXToken_Identifier:
1999 case CXToken_Keyword:
2000 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002001 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2002 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002003
2004 case CXToken_Literal: {
2005 // We have stashed the starting pointer in the ptr_data field. Use it.
2006 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002007 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002008 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002009
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002010 case CXToken_Punctuation:
2011 case CXToken_Comment:
2012 break;
2013 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002014
2015 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002016 // deconstructing the source location.
2017 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2018 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002019 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002020
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002021 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2022 std::pair<FileID, unsigned> LocInfo
2023 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
2024 std::pair<const char *,const char *> Buffer
2025 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
2026
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002027 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
2028 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002029}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002030
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002031CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2032 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2033 if (!CXXUnit)
2034 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002035
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002036 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2037 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2038}
2039
2040CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2041 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002042 if (!CXXUnit)
2043 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002044
2045 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002046 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2047}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002048
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002049void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2050 CXToken **Tokens, unsigned *NumTokens) {
2051 if (Tokens)
2052 *Tokens = 0;
2053 if (NumTokens)
2054 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002055
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002056 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2057 if (!CXXUnit || !Tokens || !NumTokens)
2058 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002059
Douglas Gregorbdf60622010-03-05 21:16:25 +00002060 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2061
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002062 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002063 if (R.isInvalid())
2064 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002065
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002066 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2067 std::pair<FileID, unsigned> BeginLocInfo
2068 = SourceMgr.getDecomposedLoc(R.getBegin());
2069 std::pair<FileID, unsigned> EndLocInfo
2070 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002071
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002072 // Cannot tokenize across files.
2073 if (BeginLocInfo.first != EndLocInfo.first)
2074 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002075
2076 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002077 std::pair<const char *,const char *> Buffer
2078 = SourceMgr.getBufferData(BeginLocInfo.first);
2079 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2080 CXXUnit->getASTContext().getLangOptions(),
2081 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2082 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002083
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002084 // Lex tokens until we hit the end of the range.
2085 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2086 llvm::SmallVector<CXToken, 32> CXTokens;
2087 Token Tok;
2088 do {
2089 // Lex the next token
2090 Lex.LexFromRawLexer(Tok);
2091 if (Tok.is(tok::eof))
2092 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002093
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002094 // Initialize the CXToken.
2095 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002096
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002097 // - Common fields
2098 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2099 CXTok.int_data[2] = Tok.getLength();
2100 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002101
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002102 // - Kind-specific fields
2103 if (Tok.isLiteral()) {
2104 CXTok.int_data[0] = CXToken_Literal;
2105 CXTok.ptr_data = (void *)Tok.getLiteralData();
2106 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002107 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002108 std::pair<FileID, unsigned> LocInfo
2109 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002110 const char *StartPos
2111 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002112 LocInfo.second;
2113 IdentifierInfo *II
2114 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2115 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2116 CXToken_Identifier
2117 : CXToken_Keyword;
2118 CXTok.ptr_data = II;
2119 } else if (Tok.is(tok::comment)) {
2120 CXTok.int_data[0] = CXToken_Comment;
2121 CXTok.ptr_data = 0;
2122 } else {
2123 CXTok.int_data[0] = CXToken_Punctuation;
2124 CXTok.ptr_data = 0;
2125 }
2126 CXTokens.push_back(CXTok);
2127 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002128
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002129 if (CXTokens.empty())
2130 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002131
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002132 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2133 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2134 *NumTokens = CXTokens.size();
2135}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002136
2137typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2138
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002139enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2140 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002141 CXClientData client_data) {
2142 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2143
2144 // We only annotate the locations of declarations, simple
2145 // references, and expressions which directly reference something.
2146 CXCursorKind Kind = clang_getCursorKind(cursor);
2147 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2148 // Okay: We can annotate the location of this declaration with the
2149 // declaration or reference
2150 } else if (clang_isExpression(cursor.kind)) {
2151 if (Kind != CXCursor_DeclRefExpr &&
2152 Kind != CXCursor_MemberRefExpr &&
2153 Kind != CXCursor_ObjCMessageExpr)
2154 return CXChildVisit_Recurse;
2155
2156 CXCursor Referenced = clang_getCursorReferenced(cursor);
2157 if (Referenced == cursor || Referenced == clang_getNullCursor())
2158 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002159
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002160 // Okay: we can annotate the location of this expression
2161 } else {
2162 // Nothing to annotate
2163 return CXChildVisit_Recurse;
2164 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002165
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002166 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2167 (*Data)[Loc.int_data] = cursor;
2168 return CXChildVisit_Recurse;
2169}
2170
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002171void clang_annotateTokens(CXTranslationUnit TU,
2172 CXToken *Tokens, unsigned NumTokens,
2173 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002174 if (NumTokens == 0)
2175 return;
2176
2177 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002178 for (unsigned I = 0; I != NumTokens; ++I)
2179 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002180
2181 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2182 if (!CXXUnit || !Tokens)
2183 return;
2184
Douglas Gregorbdf60622010-03-05 21:16:25 +00002185 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2186
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002187 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002188 SourceRange RegionOfInterest;
2189 RegionOfInterest.setBegin(
2190 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2191 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002192 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002193 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002194 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002195 // FIXME: Would be great to have a "hint" cursor, then walk from that
2196 // hint cursor upward until we find a cursor whose source range encloses
2197 // the region of interest, rather than starting from the translation unit.
2198 AnnotateTokensData Annotated;
2199 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002200 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002201 Decl::MaxPCHLevel, RegionOfInterest);
2202 AnnotateVis.VisitChildren(Parent);
2203
2204 for (unsigned I = 0; I != NumTokens; ++I) {
2205 // Determine whether we saw a cursor at this token's location.
2206 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2207 if (Pos == Annotated.end())
2208 continue;
2209
2210 Cursors[I] = Pos->second;
2211 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002212}
2213
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002214void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002215 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002216 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002217}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002218
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002219} // end: extern "C"
2220
2221//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002222// Operations for querying linkage of a cursor.
2223//===----------------------------------------------------------------------===//
2224
2225extern "C" {
2226CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
2227 Decl *D = cxcursor::getCursorDecl(cursor);
2228 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2229 switch (ND->getLinkage()) {
2230 case NoLinkage: return CXLinkage_NoLinkage;
2231 case InternalLinkage: return CXLinkage_Internal;
2232 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2233 case ExternalLinkage: return CXLinkage_External;
2234 };
2235
2236 return CXLinkage_Invalid;
2237}
2238} // end: extern "C"
2239
2240//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002241// CXString Operations.
2242//===----------------------------------------------------------------------===//
2243
2244extern "C" {
2245const char *clang_getCString(CXString string) {
2246 return string.Spelling;
2247}
2248
2249void clang_disposeString(CXString string) {
2250 if (string.MustFreeString && string.Spelling)
2251 free((void*)string.Spelling);
2252}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002253
Ted Kremenekfb480492010-01-13 21:46:36 +00002254} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002255
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002256namespace clang { namespace cxstring {
2257CXString createCXString(const char *String, bool DupString){
2258 CXString Str;
2259 if (DupString) {
2260 Str.Spelling = strdup(String);
2261 Str.MustFreeString = 1;
2262 } else {
2263 Str.Spelling = String;
2264 Str.MustFreeString = 0;
2265 }
2266 return Str;
2267}
2268
2269CXString createCXString(llvm::StringRef String, bool DupString) {
2270 CXString Result;
2271 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2272 char *Spelling = (char *)malloc(String.size() + 1);
2273 memmove(Spelling, String.data(), String.size());
2274 Spelling[String.size()] = 0;
2275 Result.Spelling = Spelling;
2276 Result.MustFreeString = 1;
2277 } else {
2278 Result.Spelling = String.data();
2279 Result.MustFreeString = 0;
2280 }
2281 return Result;
2282}
2283}}
2284
Ted Kremenek04bb7162010-01-22 22:44:15 +00002285//===----------------------------------------------------------------------===//
2286// Misc. utility functions.
2287//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002288
Ted Kremenek04bb7162010-01-22 22:44:15 +00002289extern "C" {
2290
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002291CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002292 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002293}
2294
2295} // end: extern "C"