blob: 594654fe6c6df5164f00023f6e434eb325f110aa [file] [log] [blame]
Ted Kremenekb60d87c2009-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 Dunbarbbc569c2009-11-30 20:42:43 +00007//
Ted Kremenekb60d87c2009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekb60d87c2009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000015#include "CIndexer.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremenek97a45372010-01-25 22:34:44 +000017#include "CXSourceLocation.h"
Douglas Gregor4f9c3762010-01-28 00:27:43 +000018#include "CIndexDiagnostic.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000019
Ted Kremenekc0f3f722010-01-22 22:44:15 +000020#include "clang/Basic/Version.h"
Douglas Gregorba965fb2010-01-28 00:56:43 +000021
Steve Naroffa1c72842009-08-28 15:28:48 +000022#include "clang/AST/DeclVisitor.h"
Steve Naroff66af1ae2009-09-22 19:25:29 +000023#include "clang/AST/StmtVisitor.h"
Douglas Gregor93f89952010-01-21 16:28:34 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregorba965fb2010-01-28 00:56:43 +000025#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenek2a43fd52010-01-06 23:43:31 +000026#include "clang/Lex/Lexer.h"
Douglas Gregor562c1f92010-01-22 19:49:59 +000027#include "clang/Lex/Preprocessor.h"
Douglas Gregord3d923a2009-10-16 21:24:31 +000028#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer2836c4c2009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Douglas Gregor1e21cc72010-02-18 23:07:20 +000030#include "llvm/System/Signals.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000031
Steve Naroffa1c72842009-08-28 15:28:48 +000032using namespace clang;
Ted Kremenek87553c42010-01-15 20:35:54 +000033using namespace clang::cxcursor;
Ted Kremenek5cca6eb2010-02-17 00:41:08 +000034using namespace clang::cxstring;
Steve Naroffa1c72842009-08-28 15:28:48 +000035using namespace idx;
36
Ted Kremenek991eb3f2010-01-06 03:42:32 +000037//===----------------------------------------------------------------------===//
38// Crash Reporting.
39//===----------------------------------------------------------------------===//
40
41#ifdef __APPLE__
Ted Kremenek7a5ede22010-01-07 22:49:05 +000042#define USE_CRASHTRACER
Ted Kremenek991eb3f2010-01-06 03:42:32 +000043#include "clang/Analysis/Support/SaveAndRestore.h"
44// Integrate with crash reporter.
45extern "C" const char *__crashreporter_info__;
Ted Kremenek896e1642010-02-17 21:12:23 +000046#define NUM_CRASH_STRINGS 32
Ted Kremenek7a5ede22010-01-07 22:49:05 +000047static unsigned crashtracer_counter = 0;
Ted Kremenek32b79312010-01-07 23:13:53 +000048static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek7a5ede22010-01-07 22:49:05 +000049static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
50static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
51
52static unsigned SetCrashTracerInfo(const char *str,
53 llvm::SmallString<1024> &AggStr) {
Ted Kremenekf441baf2010-02-17 00:41:40 +000054
Ted Kremenek32b79312010-01-07 23:13:53 +000055 unsigned slot = 0;
Ted Kremenek7a5ede22010-01-07 22:49:05 +000056 while (crashtracer_strings[slot]) {
57 if (++slot == NUM_CRASH_STRINGS)
58 slot = 0;
59 }
60 crashtracer_strings[slot] = str;
Ted Kremenek32b79312010-01-07 23:13:53 +000061 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek7a5ede22010-01-07 22:49:05 +000062
63 // We need to create an aggregate string because multiple threads
64 // may be in this method at one time. The crash reporter string
65 // will attempt to overapproximate the set of in-flight invocations
66 // of this function. Race conditions can still cause this goal
67 // to not be achieved.
68 {
Ted Kremenekf441baf2010-02-17 00:41:40 +000069 llvm::raw_svector_ostream Out(AggStr);
Ted Kremenek7a5ede22010-01-07 22:49:05 +000070 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
71 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
72 }
73 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
74 return slot;
75}
76
77static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek32b79312010-01-07 23:13:53 +000078 unsigned max_slot = 0;
79 unsigned max_value = 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +000080
Ted Kremenek32b79312010-01-07 23:13:53 +000081 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
82
83 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
84 if (agg_crashtracer_strings[i] &&
85 crashtracer_counter_id[i] > max_value) {
86 max_slot = i;
87 max_value = crashtracer_counter_id[i];
Ted Kremenek7a5ede22010-01-07 22:49:05 +000088 }
Ted Kremenek32b79312010-01-07 23:13:53 +000089
90 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek7a5ede22010-01-07 22:49:05 +000091}
92
93namespace {
94class ArgsCrashTracerInfo {
95 llvm::SmallString<1024> CrashString;
96 llvm::SmallString<1024> AggregateString;
97 unsigned crashtracerSlot;
98public:
99 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
100 : crashtracerSlot(0)
101 {
102 {
103 llvm::raw_svector_ostream Out(CrashString);
Ted Kremenekb3908402010-03-05 22:43:25 +0000104 Out << "ClangCIndex [" << getClangFullVersion() << "]"
105 << "[createTranslationUnitFromSourceFile]: clang";
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000106 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
107 E=Args.end(); I!=E; ++I)
108 Out << ' ' << *I;
109 }
110 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
111 AggregateString);
112 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000113
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000114 ~ArgsCrashTracerInfo() {
115 ResetCrashTracerInfo(crashtracerSlot);
116 }
117};
118}
119#endif
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000120
Douglas Gregor562c1f92010-01-22 19:49:59 +0000121/// \brief The result of comparing two source ranges.
122enum RangeComparisonResult {
123 /// \brief Either the ranges overlap or one of the ranges is invalid.
124 RangeOverlap,
Ted Kremenekf441baf2010-02-17 00:41:40 +0000125
Douglas Gregor562c1f92010-01-22 19:49:59 +0000126 /// \brief The first range ends before the second range starts.
127 RangeBefore,
Ted Kremenekf441baf2010-02-17 00:41:40 +0000128
Douglas Gregor562c1f92010-01-22 19:49:59 +0000129 /// \brief The first range starts after the second range ends.
130 RangeAfter
131};
132
Ted Kremenekf441baf2010-02-17 00:41:40 +0000133/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor562c1f92010-01-22 19:49:59 +0000134/// the translation unit.
Ted Kremenekf441baf2010-02-17 00:41:40 +0000135static RangeComparisonResult RangeCompare(SourceManager &SM,
136 SourceRange R1,
Douglas Gregor562c1f92010-01-22 19:49:59 +0000137 SourceRange R2) {
138 assert(R1.isValid() && "First range is invalid?");
139 assert(R2.isValid() && "Second range is invalid?");
Daniel Dunbar02968e52010-02-14 10:02:57 +0000140 if (R1.getEnd() == R2.getBegin() ||
141 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor562c1f92010-01-22 19:49:59 +0000142 return RangeBefore;
Daniel Dunbar02968e52010-02-14 10:02:57 +0000143 if (R2.getEnd() == R1.getBegin() ||
144 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor562c1f92010-01-22 19:49:59 +0000145 return RangeAfter;
146 return RangeOverlap;
147}
148
Daniel Dunbar474b2072010-02-14 01:47:29 +0000149/// \brief Translate a Clang source range into a CIndex source range.
150///
151/// Clang internally represents ranges where the end location points to the
152/// start of the token at the end. However, for external clients it is more
153/// useful to have a CXSourceRange be a proper half-open interval. This routine
154/// does the appropriate translation.
Ted Kremenekf441baf2010-02-17 00:41:40 +0000155CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar474b2072010-02-14 01:47:29 +0000156 const LangOptions &LangOpts,
157 SourceRange R) {
158 // FIXME: This is largely copy-paste from
159 // TextDiagnosticPrinter::HighlightRange. When it is clear that this is what
160 // we want the two routines should be refactored.
161
162 // We want the last character in this location, so we will adjust the
163 // instantiation location accordingly.
164
165 // If the location is from a macro instantiation, get the end of the
166 // instantiation range.
167 SourceLocation EndLoc = R.getEnd();
168 SourceLocation InstLoc = SM.getInstantiationLoc(EndLoc);
169 if (EndLoc.isMacroID())
170 InstLoc = SM.getInstantiationRange(EndLoc).second;
171
172 // Measure the length token we're pointing at, so we can adjust the physical
173 // location in the file to point at the last character.
174 //
175 // FIXME: This won't cope with trigraphs or escaped newlines well. For that,
176 // we actually need a preprocessor, which isn't currently available
177 // here. Eventually, we'll switch the pointer data of
178 // CXSourceLocation/CXSourceRange to a translation unit (CXXUnit), so that the
179 // preprocessor will be available here. At that point, we can use
180 // Preprocessor::getLocForEndOfToken().
181 if (InstLoc.isValid()) {
182 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, LangOpts);
Daniel Dunbar474b2072010-02-14 01:47:29 +0000183 EndLoc = EndLoc.getFileLocWithOffset(Length);
184 }
185
186 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
187 R.getBegin().getRawEncoding(),
188 EndLoc.getRawEncoding() };
189 return Result;
190}
Douglas Gregor4f46e782010-01-19 21:36:55 +0000191
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000192//===----------------------------------------------------------------------===//
Douglas Gregor562c1f92010-01-22 19:49:59 +0000193// Cursor visitor.
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000194//===----------------------------------------------------------------------===//
195
Steve Naroff1054e602009-08-31 00:59:03 +0000196namespace {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000197
Douglas Gregor71f3d942010-01-20 20:59:29 +0000198// Cursor visitor.
Douglas Gregor93f89952010-01-21 16:28:34 +0000199class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000200 public TypeLocVisitor<CursorVisitor, bool>,
201 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor93f89952010-01-21 16:28:34 +0000202{
Douglas Gregor562c1f92010-01-22 19:49:59 +0000203 /// \brief The translation unit we are traversing.
Douglas Gregorfed36b12010-01-20 23:57:43 +0000204 ASTUnit *TU;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000205
Douglas Gregor562c1f92010-01-22 19:49:59 +0000206 /// \brief The parent cursor whose children we are traversing.
Douglas Gregor71f3d942010-01-20 20:59:29 +0000207 CXCursor Parent;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000208
Douglas Gregor562c1f92010-01-22 19:49:59 +0000209 /// \brief The declaration that serves at the parent of any statement or
210 /// expression nodes.
Douglas Gregord1824312010-01-21 17:29:07 +0000211 Decl *StmtParent;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000212
Douglas Gregor562c1f92010-01-22 19:49:59 +0000213 /// \brief The visitor function.
Douglas Gregor71f3d942010-01-20 20:59:29 +0000214 CXCursorVisitor Visitor;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000215
Douglas Gregor562c1f92010-01-22 19:49:59 +0000216 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregor71f3d942010-01-20 20:59:29 +0000217 CXClientData ClientData;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000218
Douglas Gregor16bef852009-10-16 20:01:17 +0000219 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
220 // to the visitor. Declarations with a PCH level greater than this value will
221 // be suppressed.
222 unsigned MaxPCHLevel;
Douglas Gregor562c1f92010-01-22 19:49:59 +0000223
224 /// \brief When valid, a source range to which the cursor should restrict
225 /// its search.
226 SourceRange RegionOfInterest;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000227
Douglas Gregor71f3d942010-01-20 20:59:29 +0000228 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor93f89952010-01-21 16:28:34 +0000229 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000230 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000231
232 /// \brief Determine whether this particular source range comes before, comes
233 /// after, or overlaps the region of interest.
Douglas Gregor562c1f92010-01-22 19:49:59 +0000234 ///
Daniel Dunbar02968e52010-02-14 10:02:57 +0000235 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf441baf2010-02-17 00:41:40 +0000236 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
237
Steve Naroff1054e602009-08-31 00:59:03 +0000238public:
Ted Kremenekf441baf2010-02-17 00:41:40 +0000239 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
240 unsigned MaxPCHLevel,
Douglas Gregor562c1f92010-01-22 19:49:59 +0000241 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf441baf2010-02-17 00:41:40 +0000242 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Douglas Gregor562c1f92010-01-22 19:49:59 +0000243 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
Douglas Gregor71f3d942010-01-20 20:59:29 +0000244 {
245 Parent.kind = CXCursor_NoDeclFound;
246 Parent.data[0] = 0;
247 Parent.data[1] = 0;
248 Parent.data[2] = 0;
Douglas Gregord1824312010-01-21 17:29:07 +0000249 StmtParent = 0;
Douglas Gregor71f3d942010-01-20 20:59:29 +0000250 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000251
Douglas Gregor562c1f92010-01-22 19:49:59 +0000252 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregor71f3d942010-01-20 20:59:29 +0000253 bool VisitChildren(CXCursor Parent);
Ted Kremenekf441baf2010-02-17 00:41:40 +0000254
Douglas Gregor93f89952010-01-21 16:28:34 +0000255 // Declaration visitors
Ted Kremenek6ab9aa022010-02-18 05:46:33 +0000256 bool VisitAttributes(Decl *D);
Douglas Gregor71f3d942010-01-20 20:59:29 +0000257 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek7dff4162010-02-18 18:47:08 +0000258 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
259 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremeneke2110252010-02-18 22:36:18 +0000260 bool VisitTagDecl(TagDecl *D);
261 bool VisitEnumConstantDecl(EnumConstantDecl *D);
262 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
263 bool VisitFunctionDecl(FunctionDecl *ND);
264 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek7dff4162010-02-18 18:47:08 +0000265 bool VisitVarDecl(VarDecl *);
Ted Kremeneke2110252010-02-18 22:36:18 +0000266 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
267 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
268 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
269 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
270 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
271 bool VisitObjCImplDecl(ObjCImplDecl *D);
272 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
273 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
274 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
275 // etc.
276 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
277 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
278 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor93f89952010-01-21 16:28:34 +0000279
280 // Type visitors
Douglas Gregord1824312010-01-21 17:29:07 +0000281 // FIXME: QualifiedTypeLoc doesn't provide any location information
282 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor93f89952010-01-21 16:28:34 +0000283 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregord1824312010-01-21 17:29:07 +0000284 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
285 bool VisitTagTypeLoc(TagTypeLoc TL);
286 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
287 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
288 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
289 bool VisitPointerTypeLoc(PointerTypeLoc TL);
290 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
291 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
292 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
293 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
294 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
295 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor6479fc42010-01-21 20:48:56 +0000296 // FIXME: Implement for TemplateSpecializationTypeLoc
297 // FIXME: Implement visitors here when the unimplemented TypeLocs get
298 // implemented
299 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
300 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf441baf2010-02-17 00:41:40 +0000301
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000302 // Statement visitors
303 bool VisitStmt(Stmt *S);
304 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregore1084fa2010-01-22 01:00:11 +0000305 // FIXME: LabelStmt label?
306 bool VisitIfStmt(IfStmt *S);
307 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000308 bool VisitWhileStmt(WhileStmt *S);
309 bool VisitForStmt(ForStmt *S);
Ted Kremenekf441baf2010-02-17 00:41:40 +0000310
Douglas Gregor625a5152010-01-23 00:40:08 +0000311 // Expression visitors
312 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
313 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
314 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorde4827d2010-03-08 16:40:19 +0000315 bool VisitObjCMessageExpr(ObjCMessageExpr *E);
Steve Naroff1054e602009-08-31 00:59:03 +0000316};
Ted Kremenekf441baf2010-02-17 00:41:40 +0000317
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000318} // end anonymous namespace
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000319
Douglas Gregor562c1f92010-01-22 19:49:59 +0000320RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000321 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
322}
323
Douglas Gregor71f3d942010-01-20 20:59:29 +0000324/// \brief Visit the given cursor and, if requested by the visitor,
325/// its children.
326///
Douglas Gregor562c1f92010-01-22 19:49:59 +0000327/// \param Cursor the cursor to visit.
328///
329/// \param CheckRegionOfInterest if true, then the caller already checked that
330/// this cursor is within the region of interest.
331///
Douglas Gregor71f3d942010-01-20 20:59:29 +0000332/// \returns true if the visitation should be aborted, false if it
333/// should continue.
Douglas Gregor562c1f92010-01-22 19:49:59 +0000334bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregor71f3d942010-01-20 20:59:29 +0000335 if (clang_isInvalid(Cursor.kind))
336 return false;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000337
Douglas Gregor71f3d942010-01-20 20:59:29 +0000338 if (clang_isDeclaration(Cursor.kind)) {
339 Decl *D = getCursorDecl(Cursor);
340 assert(D && "Invalid declaration cursor");
341 if (D->getPCHLevel() > MaxPCHLevel)
342 return false;
343
344 if (D->isImplicit())
345 return false;
346 }
347
Douglas Gregor562c1f92010-01-22 19:49:59 +0000348 // If we have a range of interest, and this cursor doesn't intersect with it,
349 // we're done.
350 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbar2f4ba172010-02-14 08:32:05 +0000351 SourceRange Range =
352 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
353 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor562c1f92010-01-22 19:49:59 +0000354 return false;
355 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000356
Douglas Gregor71f3d942010-01-20 20:59:29 +0000357 switch (Visitor(Cursor, Parent, ClientData)) {
358 case CXChildVisit_Break:
359 return true;
360
361 case CXChildVisit_Continue:
362 return false;
363
364 case CXChildVisit_Recurse:
365 return VisitChildren(Cursor);
366 }
367
Douglas Gregor33f16852010-01-25 16:45:46 +0000368 return false;
Douglas Gregor71f3d942010-01-20 20:59:29 +0000369}
370
371/// \brief Visit the children of the given cursor.
372///
373/// \returns true if the visitation should be aborted, false if it
374/// should continue.
Ted Kremenekf441baf2010-02-17 00:41:40 +0000375bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000376 if (clang_isReference(Cursor.kind)) {
377 // By definition, references have no children.
378 return false;
379 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000380
381 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregor71f3d942010-01-20 20:59:29 +0000382 // done.
383 class SetParentRAII {
384 CXCursor &Parent;
Douglas Gregord1824312010-01-21 17:29:07 +0000385 Decl *&StmtParent;
Douglas Gregor71f3d942010-01-20 20:59:29 +0000386 CXCursor OldParent;
Douglas Gregord1824312010-01-21 17:29:07 +0000387
Douglas Gregor71f3d942010-01-20 20:59:29 +0000388 public:
Douglas Gregord1824312010-01-21 17:29:07 +0000389 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf441baf2010-02-17 00:41:40 +0000390 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregor71f3d942010-01-20 20:59:29 +0000391 {
392 Parent = NewParent;
Douglas Gregord1824312010-01-21 17:29:07 +0000393 if (clang_isDeclaration(Parent.kind))
394 StmtParent = getCursorDecl(Parent);
Douglas Gregor71f3d942010-01-20 20:59:29 +0000395 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000396
Douglas Gregor71f3d942010-01-20 20:59:29 +0000397 ~SetParentRAII() {
398 Parent = OldParent;
Douglas Gregord1824312010-01-21 17:29:07 +0000399 if (clang_isDeclaration(Parent.kind))
400 StmtParent = getCursorDecl(Parent);
Douglas Gregor71f3d942010-01-20 20:59:29 +0000401 }
Douglas Gregord1824312010-01-21 17:29:07 +0000402 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf441baf2010-02-17 00:41:40 +0000403
Douglas Gregor71f3d942010-01-20 20:59:29 +0000404 if (clang_isDeclaration(Cursor.kind)) {
405 Decl *D = getCursorDecl(Cursor);
406 assert(D && "Invalid declaration cursor");
Ted Kremenek0e293092010-02-18 18:47:01 +0000407 return VisitAttributes(D) || Visit(D);
Douglas Gregor71f3d942010-01-20 20:59:29 +0000408 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000409
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000410 if (clang_isStatement(Cursor.kind))
411 return Visit(getCursorStmt(Cursor));
412 if (clang_isExpression(Cursor.kind))
413 return Visit(getCursorExpr(Cursor));
Ted Kremenekf441baf2010-02-17 00:41:40 +0000414
Douglas Gregor71f3d942010-01-20 20:59:29 +0000415 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000416 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor562c1f92010-01-22 19:49:59 +0000417 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
418 RegionOfInterest.isInvalid()) {
Douglas Gregorbefc4a12010-01-20 21:13:59 +0000419 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
420 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
421 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000422 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregorbefc4a12010-01-20 21:13:59 +0000423 return true;
424 }
425 } else {
426 return VisitDeclContext(
Douglas Gregor71f3d942010-01-20 20:59:29 +0000427 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregorbefc4a12010-01-20 21:13:59 +0000428 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000429
Douglas Gregorbefc4a12010-01-20 21:13:59 +0000430 return false;
Douglas Gregor71f3d942010-01-20 20:59:29 +0000431 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000432
Douglas Gregor71f3d942010-01-20 20:59:29 +0000433 // Nothing to visit at the moment.
Douglas Gregor71f3d942010-01-20 20:59:29 +0000434 return false;
435}
436
Douglas Gregor71f3d942010-01-20 20:59:29 +0000437bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenek78668fd2010-01-13 00:22:49 +0000438 for (DeclContext::decl_iterator
Douglas Gregor71f3d942010-01-20 20:59:29 +0000439 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek6ab9aa022010-02-18 05:46:33 +0000440
Daniel Dunbar02968e52010-02-14 10:02:57 +0000441 CXCursor Cursor = MakeCXCursor(*I, TU);
442
Douglas Gregor562c1f92010-01-22 19:49:59 +0000443 if (RegionOfInterest.isValid()) {
Daniel Dunbar02968e52010-02-14 10:02:57 +0000444 SourceRange Range =
445 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
446 if (Range.isInvalid())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000447 continue;
Daniel Dunbar02968e52010-02-14 10:02:57 +0000448
449 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000450 case RangeBefore:
451 // This declaration comes before the region of interest; skip it.
452 continue;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000453
Douglas Gregor562c1f92010-01-22 19:49:59 +0000454 case RangeAfter:
455 // This declaration comes after the region of interest; we're done.
456 return false;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000457
Douglas Gregor562c1f92010-01-22 19:49:59 +0000458 case RangeOverlap:
459 // This declaration overlaps the region of interest; visit it.
460 break;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000461 }
Douglas Gregor562c1f92010-01-22 19:49:59 +0000462 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000463
Daniel Dunbar02968e52010-02-14 10:02:57 +0000464 if (Visit(Cursor, true))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000465 return true;
466 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000467
Douglas Gregor71f3d942010-01-20 20:59:29 +0000468 return false;
Ted Kremenek78668fd2010-01-13 00:22:49 +0000469}
470
Douglas Gregord824f882010-01-22 00:50:27 +0000471bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
472 llvm_unreachable("Translation units are visited directly by Visit()");
473 return false;
474}
475
476bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
477 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
478 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf441baf2010-02-17 00:41:40 +0000479
Douglas Gregord824f882010-01-22 00:50:27 +0000480 return false;
481}
482
483bool CursorVisitor::VisitTagDecl(TagDecl *D) {
484 return VisitDeclContext(D);
485}
486
487bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
488 if (Expr *Init = D->getInitExpr())
489 return Visit(MakeCXCursor(Init, StmtParent, TU));
490 return false;
491}
492
Douglas Gregor93f89952010-01-21 16:28:34 +0000493bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
494 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
495 if (Visit(TSInfo->getTypeLoc()))
496 return true;
497
498 return false;
499}
500
Douglas Gregor71f3d942010-01-20 20:59:29 +0000501bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregord1824312010-01-21 17:29:07 +0000502 if (VisitDeclaratorDecl(ND))
503 return true;
504
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000505 if (ND->isThisDeclarationADefinition() &&
506 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
507 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000508
Douglas Gregor71f3d942010-01-20 20:59:29 +0000509 return false;
510}
Ted Kremenek78668fd2010-01-13 00:22:49 +0000511
Douglas Gregord824f882010-01-22 00:50:27 +0000512bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
513 if (VisitDeclaratorDecl(D))
514 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000515
Douglas Gregord824f882010-01-22 00:50:27 +0000516 if (Expr *BitWidth = D->getBitWidth())
517 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf441baf2010-02-17 00:41:40 +0000518
Douglas Gregord824f882010-01-22 00:50:27 +0000519 return false;
520}
521
522bool CursorVisitor::VisitVarDecl(VarDecl *D) {
523 if (VisitDeclaratorDecl(D))
524 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000525
Douglas Gregord824f882010-01-22 00:50:27 +0000526 if (Expr *Init = D->getInit())
527 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf441baf2010-02-17 00:41:40 +0000528
Douglas Gregord824f882010-01-22 00:50:27 +0000529 return false;
530}
531
532bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor12852d92010-03-08 14:59:44 +0000533 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
534 if (Visit(TSInfo->getTypeLoc()))
535 return true;
536
Ted Kremenekf441baf2010-02-17 00:41:40 +0000537 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregord824f882010-01-22 00:50:27 +0000538 PEnd = ND->param_end();
539 P != PEnd; ++P) {
540 if (Visit(MakeCXCursor(*P, TU)))
541 return true;
542 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000543
Douglas Gregord824f882010-01-22 00:50:27 +0000544 if (ND->isThisDeclarationADefinition() &&
545 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
546 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000547
Douglas Gregord824f882010-01-22 00:50:27 +0000548 return false;
549}
550
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000551bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
552 return VisitDeclContext(D);
553}
554
Douglas Gregor71f3d942010-01-20 20:59:29 +0000555bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000556 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
557 TU)))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000558 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000559
Douglas Gregoref6eb842010-01-16 15:44:18 +0000560 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
561 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
562 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorfed36b12010-01-20 23:57:43 +0000563 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000564 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000565
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000566 return VisitObjCContainerDecl(ND);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000567}
568
Douglas Gregord824f882010-01-22 00:50:27 +0000569bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
570 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
571 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
572 E = PID->protocol_end(); I != E; ++I, ++PL)
573 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
574 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000575
Douglas Gregord824f882010-01-22 00:50:27 +0000576 return VisitObjCContainerDecl(PID);
577}
578
Douglas Gregor71f3d942010-01-20 20:59:29 +0000579bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenek78668fd2010-01-13 00:22:49 +0000580 // Issue callbacks for super class.
Douglas Gregor71f3d942010-01-20 20:59:29 +0000581 if (D->getSuperClass() &&
582 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf441baf2010-02-17 00:41:40 +0000583 D->getSuperClassLoc(),
Douglas Gregorfed36b12010-01-20 23:57:43 +0000584 TU)))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000585 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000586
Douglas Gregoref6eb842010-01-16 15:44:18 +0000587 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
588 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
589 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorfed36b12010-01-20 23:57:43 +0000590 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000591 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000592
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000593 return VisitObjCContainerDecl(D);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000594}
595
Douglas Gregord824f882010-01-22 00:50:27 +0000596bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
597 return VisitObjCContainerDecl(D);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000598}
599
Douglas Gregord824f882010-01-22 00:50:27 +0000600bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000601 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregord824f882010-01-22 00:50:27 +0000602 D->getLocation(), TU)))
603 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000604
Douglas Gregord824f882010-01-22 00:50:27 +0000605 return VisitObjCImplDecl(D);
606}
607
608bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
609#if 0
610 // Issue callbacks for super class.
611 // FIXME: No source location information!
612 if (D->getSuperClass() &&
613 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf441baf2010-02-17 00:41:40 +0000614 D->getSuperClassLoc(),
Douglas Gregord824f882010-01-22 00:50:27 +0000615 TU)))
616 return true;
617#endif
Ted Kremenekf441baf2010-02-17 00:41:40 +0000618
Douglas Gregord824f882010-01-22 00:50:27 +0000619 return VisitObjCImplDecl(D);
620}
621
622bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
623 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
624 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
625 E = D->protocol_end();
626 I != E; ++I, ++PL)
Douglas Gregorfed36b12010-01-20 23:57:43 +0000627 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregor71f3d942010-01-20 20:59:29 +0000628 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000629
630 return false;
Ted Kremenek78668fd2010-01-13 00:22:49 +0000631}
632
Douglas Gregord824f882010-01-22 00:50:27 +0000633bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
634 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
635 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
636 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000637
Douglas Gregord824f882010-01-22 00:50:27 +0000638 return false;
Ted Kremenek78668fd2010-01-13 00:22:49 +0000639}
640
Douglas Gregord1824312010-01-21 17:29:07 +0000641bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
642 ASTContext &Context = TU->getASTContext();
643
644 // Some builtin types (such as Objective-C's "id", "sel", and
645 // "Class") have associated declarations. Create cursors for those.
646 QualType VisitType;
647 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000648 case BuiltinType::Void:
Douglas Gregord1824312010-01-21 17:29:07 +0000649 case BuiltinType::Bool:
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000650 case BuiltinType::Char_U:
651 case BuiltinType::UChar:
Douglas Gregord1824312010-01-21 17:29:07 +0000652 case BuiltinType::Char16:
653 case BuiltinType::Char32:
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000654 case BuiltinType::UShort:
Douglas Gregord1824312010-01-21 17:29:07 +0000655 case BuiltinType::UInt:
656 case BuiltinType::ULong:
657 case BuiltinType::ULongLong:
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000658 case BuiltinType::UInt128:
659 case BuiltinType::Char_S:
660 case BuiltinType::SChar:
Douglas Gregord1824312010-01-21 17:29:07 +0000661 case BuiltinType::WChar:
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000662 case BuiltinType::Short:
663 case BuiltinType::Int:
664 case BuiltinType::Long:
665 case BuiltinType::LongLong:
666 case BuiltinType::Int128:
667 case BuiltinType::Float:
668 case BuiltinType::Double:
669 case BuiltinType::LongDouble:
670 case BuiltinType::NullPtr:
671 case BuiltinType::Overload:
672 case BuiltinType::Dependent:
Douglas Gregord1824312010-01-21 17:29:07 +0000673 break;
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000674
675 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregord1824312010-01-21 17:29:07 +0000676 break;
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000677
Ted Kremenekfcb3db72010-02-18 18:52:18 +0000678 case BuiltinType::ObjCId:
679 VisitType = Context.getObjCIdType();
680 break;
Ted Kremenekaaaf2bf2010-02-18 22:32:43 +0000681
682 case BuiltinType::ObjCClass:
683 VisitType = Context.getObjCClassType();
684 break;
685
Douglas Gregord1824312010-01-21 17:29:07 +0000686 case BuiltinType::ObjCSel:
687 VisitType = Context.getObjCSelType();
688 break;
689 }
690
691 if (!VisitType.isNull()) {
692 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf441baf2010-02-17 00:41:40 +0000693 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregord1824312010-01-21 17:29:07 +0000694 TU));
695 }
696
697 return false;
698}
699
Douglas Gregor93f89952010-01-21 16:28:34 +0000700bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
701 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
702}
703
Douglas Gregord1824312010-01-21 17:29:07 +0000704bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
705 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
706}
707
708bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
709 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
710}
711
712bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
713 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
714 return true;
715
716 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
717 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
718 TU)))
719 return true;
720 }
721
722 return false;
723}
724
725bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
726 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
727 return true;
728
729 if (TL.hasProtocolsAsWritten()) {
730 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000731 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregord1824312010-01-21 17:29:07 +0000732 TL.getProtocolLoc(I),
733 TU)))
734 return true;
735 }
736 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000737
Douglas Gregord1824312010-01-21 17:29:07 +0000738 return false;
739}
740
741bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
742 return Visit(TL.getPointeeLoc());
743}
744
745bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
746 return Visit(TL.getPointeeLoc());
747}
748
749bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
750 return Visit(TL.getPointeeLoc());
751}
752
753bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000754 return Visit(TL.getPointeeLoc());
Douglas Gregord1824312010-01-21 17:29:07 +0000755}
756
757bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000758 return Visit(TL.getPointeeLoc());
Douglas Gregord1824312010-01-21 17:29:07 +0000759}
760
761bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
762 if (Visit(TL.getResultLoc()))
763 return true;
764
Douglas Gregord1824312010-01-21 17:29:07 +0000765 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
766 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
767 return true;
768
769 return false;
770}
771
772bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
773 if (Visit(TL.getElementLoc()))
774 return true;
775
776 if (Expr *Size = TL.getSizeExpr())
777 return Visit(MakeCXCursor(Size, StmtParent, TU));
778
779 return false;
780}
781
Douglas Gregor6479fc42010-01-21 20:48:56 +0000782bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
783 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
784}
785
786bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
787 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
788 return Visit(TSInfo->getTypeLoc());
789
790 return false;
791}
792
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000793bool CursorVisitor::VisitStmt(Stmt *S) {
794 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
795 Child != ChildEnd; ++Child) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000796 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000797 return true;
798 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000799
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000800 return false;
801}
802
803bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
804 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
805 D != DEnd; ++D) {
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000806 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000807 return true;
808 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000809
Douglas Gregor5e8cf372010-01-21 23:27:09 +0000810 return false;
811}
812
Douglas Gregore1084fa2010-01-22 01:00:11 +0000813bool CursorVisitor::VisitIfStmt(IfStmt *S) {
814 if (VarDecl *Var = S->getConditionVariable()) {
815 if (Visit(MakeCXCursor(Var, TU)))
816 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000817 }
818
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000819 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
820 return true;
Douglas Gregore1084fa2010-01-22 01:00:11 +0000821 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
822 return true;
Douglas Gregore1084fa2010-01-22 01:00:11 +0000823 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
824 return true;
825
826 return false;
827}
828
829bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
830 if (VarDecl *Var = S->getConditionVariable()) {
831 if (Visit(MakeCXCursor(Var, TU)))
832 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000833 }
834
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000835 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
836 return true;
837 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
838 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000839
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000840 return false;
841}
842
843bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
844 if (VarDecl *Var = S->getConditionVariable()) {
845 if (Visit(MakeCXCursor(Var, TU)))
846 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000847 }
848
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000849 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
850 return true;
851 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregore1084fa2010-01-22 01:00:11 +0000852 return true;
853
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000854 return false;
855}
856
857bool CursorVisitor::VisitForStmt(ForStmt *S) {
858 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
859 return true;
860 if (VarDecl *Var = S->getConditionVariable()) {
861 if (Visit(MakeCXCursor(Var, TU)))
862 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000863 }
864
Douglas Gregor7201f9f2010-01-25 16:12:32 +0000865 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
866 return true;
867 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
868 return true;
Douglas Gregore1084fa2010-01-22 01:00:11 +0000869 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
870 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000871
Douglas Gregore1084fa2010-01-22 01:00:11 +0000872 return false;
873}
874
Douglas Gregor625a5152010-01-23 00:40:08 +0000875bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
876 if (E->isArgumentType()) {
877 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
878 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf441baf2010-02-17 00:41:40 +0000879
Douglas Gregor625a5152010-01-23 00:40:08 +0000880 return false;
881 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000882
Douglas Gregor625a5152010-01-23 00:40:08 +0000883 return VisitExpr(E);
884}
885
886bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
887 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
888 if (Visit(TSInfo->getTypeLoc()))
889 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000890
Douglas Gregor625a5152010-01-23 00:40:08 +0000891 return VisitCastExpr(E);
892}
893
894bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
895 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
896 if (Visit(TSInfo->getTypeLoc()))
897 return true;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000898
Douglas Gregor625a5152010-01-23 00:40:08 +0000899 return VisitExpr(E);
900}
901
Douglas Gregorde4827d2010-03-08 16:40:19 +0000902bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
903 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
904 if (CI.Decl && Visit(MakeCursorObjCClassRef(CI.Decl, CI.Loc, TU)))
905 return true;
906
907 return VisitExpr(E);
908}
909
Ted Kremenek6ab9aa022010-02-18 05:46:33 +0000910bool CursorVisitor::VisitAttributes(Decl *D) {
911 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
912 if (Visit(MakeCXCursor(A, D, TU)))
913 return true;
914
915 return false;
916}
917
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000918extern "C" {
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000919CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
920 int displayDiagnostics) {
Douglas Gregor87752492010-01-22 20:35:53 +0000921 CIndexer *CIdxr = new CIndexer();
Steve Naroff531e2842009-10-20 14:46:24 +0000922 if (excludeDeclarationsFromPCH)
923 CIdxr->setOnlyLocalDecls();
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000924 if (displayDiagnostics)
925 CIdxr->setDisplayDiagnostics();
Steve Naroff531e2842009-10-20 14:46:24 +0000926 return CIdxr;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000927}
928
Daniel Dunbar079203f2009-12-01 03:14:51 +0000929void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +0000930 if (CIdx)
931 delete static_cast<CIndexer *>(CIdx);
Steve Naroff3aa2d732009-09-17 18:33:27 +0000932}
933
Daniel Dunbar11089662009-12-03 01:54:28 +0000934void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +0000935 if (CIdx) {
936 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
937 CXXIdx->setUseExternalASTGeneration(value);
938 }
Daniel Dunbar11089662009-12-03 01:54:28 +0000939}
940
Daniel Dunbar079203f2009-12-01 03:14:51 +0000941CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000942 const char *ast_filename) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +0000943 if (!CIdx)
944 return 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000945
Douglas Gregor16bef852009-10-16 20:01:17 +0000946 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000947
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000948 // Configure the diagnostics.
949 DiagnosticOptions DiagOpts;
950 llvm::OwningPtr<Diagnostic> Diags;
951 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000952 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000953 CXXIdx->getOnlyLocalDecls(),
954 0, 0, true);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000955}
956
Daniel Dunbar079203f2009-12-01 03:14:51 +0000957CXTranslationUnit
958clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
959 const char *source_filename,
960 int num_command_line_args,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000961 const char **command_line_args,
962 unsigned num_unsaved_files,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000963 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +0000964 if (!CIdx)
965 return 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +0000966
Steve Naroff531e2842009-10-20 14:46:24 +0000967 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
968
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000969 // Configure the diagnostics.
970 DiagnosticOptions DiagOpts;
971 llvm::OwningPtr<Diagnostic> Diags;
972 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf441baf2010-02-17 00:41:40 +0000973
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000974 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
975 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf441baf2010-02-17 00:41:40 +0000976 const llvm::MemoryBuffer *Buffer
Douglas Gregor89a56c52010-02-27 01:32:48 +0000977 = llvm::MemoryBuffer::getMemBufferCopy(unsaved_files[I].Contents,
Douglas Gregor6cb5ba42010-02-18 23:35:40 +0000978 unsaved_files[I].Contents + unsaved_files[I].Length,
979 unsaved_files[I].Filename);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000980 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
981 Buffer));
982 }
Ted Kremenekf441baf2010-02-17 00:41:40 +0000983
Daniel Dunbar11089662009-12-03 01:54:28 +0000984 if (!CXXIdx->getUseExternalASTGeneration()) {
985 llvm::SmallVector<const char *, 16> Args;
986
987 // The 'source_filename' argument is optional. If the caller does not
988 // specify it then it is assumed that the source file is specified
989 // in the actual argument list.
990 if (source_filename)
991 Args.push_back(source_filename);
992 Args.insert(Args.end(), command_line_args,
993 command_line_args + num_command_line_args);
994
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000995 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf441baf2010-02-17 00:41:40 +0000996
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000997#ifdef USE_CRASHTRACER
998 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000999#endif
Ted Kremenekf441baf2010-02-17 00:41:40 +00001000
Daniel Dunbar72fe5b12009-12-05 02:17:18 +00001001 llvm::OwningPtr<ASTUnit> Unit(
1002 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf441baf2010-02-17 00:41:40 +00001003 *Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +00001004 CXXIdx->getClangResourcesPath(),
Daniel Dunbar72fe5b12009-12-05 02:17:18 +00001005 CXXIdx->getOnlyLocalDecls(),
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001006 RemappedFiles.data(),
Douglas Gregor33cdd812010-02-18 18:08:43 +00001007 RemappedFiles.size(),
1008 /*CaptureDiagnostics=*/true));
Ted Kremenekf441baf2010-02-17 00:41:40 +00001009
Daniel Dunbar72fe5b12009-12-05 02:17:18 +00001010 // FIXME: Until we have broader testing, just drop the entire AST if we
1011 // encountered an error.
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001012 if (NumErrors != Diags->getNumErrors()) {
Ted Kremeneka6d3ab32010-03-05 22:43:29 +00001013 // Make sure to check that 'Unit' is non-NULL.
1014 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001015 for (ASTUnit::diag_iterator D = Unit->diag_begin(),
1016 DEnd = Unit->diag_end();
1017 D != DEnd; ++D) {
1018 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregord770f732010-02-22 23:17:23 +00001019 CXString Msg = clang_formatDiagnostic(&Diag,
1020 clang_defaultDiagnosticDisplayOptions());
1021 fprintf(stderr, "%s\n", clang_getCString(Msg));
1022 clang_disposeString(Msg);
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001023 }
Douglas Gregord770f732010-02-22 23:17:23 +00001024#ifdef LLVM_ON_WIN32
1025 // On Windows, force a flush, since there may be multiple copies of
1026 // stderr and stdout in the file system, all with different buffers
1027 // but writing to the same device.
1028 fflush(stderr);
1029#endif
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001030 }
Daniel Dunbar72fe5b12009-12-05 02:17:18 +00001031 return 0;
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001032 }
Daniel Dunbar72fe5b12009-12-05 02:17:18 +00001033
1034 return Unit.take();
Daniel Dunbar11089662009-12-03 01:54:28 +00001035 }
1036
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001037 // Build up the arguments for invoking 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +00001038 std::vector<const char *> argv;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001039
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001040 // First add the complete path to the 'clang' executable.
1041 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +00001042 argv.push_back(ClangPath.c_str());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001043
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001044 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +00001045 argv.push_back("-emit-ast");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001046
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001047 // The 'source_filename' argument is optional. If the caller does not
1048 // specify it then it is assumed that the source file is specified
1049 // in the actual argument list.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001050 if (source_filename)
1051 argv.push_back(source_filename);
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001052
Steve Naroff1cfb96c2009-10-15 20:50:09 +00001053 // Generate a temporary name for the AST file.
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001054 argv.push_back("-o");
Benjamin Kramer84c37f92010-03-13 13:05:20 +00001055 llvm::sys::Path astTmpFile(CIndexer::getTemporaryPath());
1056 argv.push_back(astTmpFile.c_str());
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001057
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001058 // Remap any unsaved files to temporary files.
1059 std::vector<llvm::sys::Path> TemporaryFiles;
1060 std::vector<std::string> RemapArgs;
1061 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1062 return 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001063
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001064 // The pointers into the elements of RemapArgs are stable because we
1065 // won't be adding anything to RemapArgs after this point.
1066 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1067 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001068
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001069 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1070 for (int i = 0; i < num_command_line_args; ++i)
1071 if (const char *arg = command_line_args[i]) {
1072 if (strcmp(arg, "-o") == 0) {
1073 ++i; // Also skip the matching argument.
1074 continue;
1075 }
1076 if (strcmp(arg, "-emit-ast") == 0 ||
1077 strcmp(arg, "-c") == 0 ||
1078 strcmp(arg, "-fsyntax-only") == 0) {
1079 continue;
1080 }
1081
1082 // Keep the argument.
1083 argv.push_back(arg);
Steve Naroff531e2842009-10-20 14:46:24 +00001084 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001085
Douglas Gregorac0605e2010-01-28 06:00:51 +00001086 // Generate a temporary name for the diagnostics file.
Benjamin Kramer84c37f92010-03-13 13:05:20 +00001087 llvm::sys::Path DiagnosticsFile(CIndexer::getTemporaryPath());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001088 TemporaryFiles.push_back(DiagnosticsFile);
1089 argv.push_back("-fdiagnostics-binary");
1090
Ted Kremenek649bf5c2009-10-22 00:03:57 +00001091 // Add the null terminator.
Ted Kremenek51d06bb2009-10-15 23:21:22 +00001092 argv.push_back(NULL);
1093
Ted Kremenek12e678d2009-10-26 22:14:08 +00001094 // Invoke 'clang'.
1095 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1096 // on Unix or NUL (Windows).
Ted Kremenek44886fd2009-10-22 03:24:01 +00001097 std::string ErrMsg;
Douglas Gregorac0605e2010-01-28 06:00:51 +00001098 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1099 NULL };
Ted Kremenek44886fd2009-10-22 03:24:01 +00001100 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregorba965fb2010-01-28 00:56:43 +00001101 /* redirects */ &Redirects[0],
Ted Kremenek44886fd2009-10-22 03:24:01 +00001102 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001103
Douglas Gregorba965fb2010-01-28 00:56:43 +00001104 if (!ErrMsg.empty()) {
1105 std::string AllArgs;
Ted Kremenek44886fd2009-10-22 03:24:01 +00001106 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregorba965fb2010-01-28 00:56:43 +00001107 I != E; ++I) {
1108 AllArgs += ' ';
Ted Kremenekbf0690c2009-10-26 22:08:39 +00001109 if (*I)
Douglas Gregorba965fb2010-01-28 00:56:43 +00001110 AllArgs += *I;
Ted Kremenekbf0690c2009-10-26 22:08:39 +00001111 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001112
Daniel Dunbar253dbad2010-02-23 20:23:45 +00001113 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek44886fd2009-10-22 03:24:01 +00001114 }
Benjamin Kramer2836c4c2009-10-18 11:19:36 +00001115
Benjamin Kramer84c37f92010-03-13 13:05:20 +00001116 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile.str(), *Diags,
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001117 CXXIdx->getOnlyLocalDecls(),
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001118 RemappedFiles.data(),
Douglas Gregor33cdd812010-02-18 18:08:43 +00001119 RemappedFiles.size(),
1120 /*CaptureDiagnostics=*/true);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001121 if (ATU) {
1122 LoadSerializedDiagnostics(DiagnosticsFile,
1123 num_unsaved_files, unsaved_files,
1124 ATU->getFileManager(),
1125 ATU->getSourceManager(),
1126 ATU->getDiagnostics());
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001127 } else if (CXXIdx->getDisplayDiagnostics()) {
1128 // We failed to load the ASTUnit, but we can still deserialize the
1129 // diagnostics and emit them.
1130 FileManager FileMgr;
1131 SourceManager SourceMgr;
1132 // FIXME: Faked LangOpts!
1133 LangOptions LangOpts;
1134 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1135 LoadSerializedDiagnostics(DiagnosticsFile,
1136 num_unsaved_files, unsaved_files,
1137 FileMgr, SourceMgr, Diags);
1138 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1139 DEnd = Diags.end();
1140 D != DEnd; ++D) {
1141 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregord770f732010-02-22 23:17:23 +00001142 CXString Msg = clang_formatDiagnostic(&Diag,
1143 clang_defaultDiagnosticDisplayOptions());
1144 fprintf(stderr, "%s\n", clang_getCString(Msg));
1145 clang_disposeString(Msg);
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001146 }
Douglas Gregord770f732010-02-22 23:17:23 +00001147
1148#ifdef LLVM_ON_WIN32
1149 // On Windows, force a flush, since there may be multiple copies of
1150 // stderr and stdout in the file system, all with different buffers
1151 // but writing to the same device.
1152 fflush(stderr);
1153#endif
Douglas Gregor33cdd812010-02-18 18:08:43 +00001154 }
Douglas Gregorac0605e2010-01-28 06:00:51 +00001155
Douglas Gregor6cb5ba42010-02-18 23:35:40 +00001156 if (ATU) {
1157 // Make the translation unit responsible for destroying all temporary files.
1158 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1159 ATU->addTemporaryFile(TemporaryFiles[i]);
1160 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1161 } else {
1162 // Destroy all of the temporary files now; they can't be referenced any
1163 // longer.
1164 llvm::sys::Path(astTmpFile).eraseFromDisk();
1165 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1166 TemporaryFiles[i].eraseFromDisk();
1167 }
1168
Steve Naroff44cd60e2009-10-15 22:23:48 +00001169 return ATU;
Steve Naroff7781daa2009-10-15 20:04:39 +00001170}
1171
Daniel Dunbar079203f2009-12-01 03:14:51 +00001172void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +00001173 if (CTUnit)
1174 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff3aa2d732009-09-17 18:33:27 +00001175}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001176
Daniel Dunbar079203f2009-12-01 03:14:51 +00001177CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor69ff5dc2010-01-29 00:47:48 +00001178 if (!CTUnit)
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001179 return createCXString("");
Ted Kremenekf441baf2010-02-17 00:41:40 +00001180
Steve Naroffc0683b92009-09-03 18:19:54 +00001181 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001182 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroff38c1a7b2009-09-03 15:49:00 +00001183}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +00001184
Douglas Gregord2fc7272010-01-20 00:23:15 +00001185CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorfed36b12010-01-20 23:57:43 +00001186 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregord2fc7272010-01-20 00:23:15 +00001187 return Result;
1188}
1189
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001190} // end: extern "C"
Steve Naroffd5e8e862009-08-27 19:51:58 +00001191
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001192//===----------------------------------------------------------------------===//
Douglas Gregor4f46e782010-01-19 21:36:55 +00001193// CXSourceLocation and CXSourceRange Operations.
1194//===----------------------------------------------------------------------===//
1195
Douglas Gregor816fd362010-01-22 21:44:22 +00001196extern "C" {
1197CXSourceLocation clang_getNullLocation() {
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001198 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregor816fd362010-01-22 21:44:22 +00001199 return Result;
1200}
1201
1202unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar83a23542010-01-30 23:58:27 +00001203 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1204 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1205 loc1.int_data == loc2.int_data);
Douglas Gregor816fd362010-01-22 21:44:22 +00001206}
1207
1208CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1209 CXFile file,
1210 unsigned line,
1211 unsigned column) {
1212 if (!tu)
1213 return clang_getNullLocation();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001214
Douglas Gregor816fd362010-01-22 21:44:22 +00001215 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1216 SourceLocation SLoc
1217 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf441baf2010-02-17 00:41:40 +00001218 static_cast<const FileEntry *>(file),
Douglas Gregor816fd362010-01-22 21:44:22 +00001219 line, column);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001220
Daniel Dunbarc4b4d392010-02-14 01:47:36 +00001221 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregor816fd362010-01-22 21:44:22 +00001222}
1223
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001224CXSourceRange clang_getNullRange() {
1225 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1226 return Result;
1227}
Daniel Dunbar02968e52010-02-14 10:02:57 +00001228
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001229CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1230 if (begin.ptr_data[0] != end.ptr_data[0] ||
1231 begin.ptr_data[1] != end.ptr_data[1])
1232 return clang_getNullRange();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001233
1234 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001235 begin.int_data, end.int_data };
Douglas Gregor816fd362010-01-22 21:44:22 +00001236 return Result;
1237}
1238
Douglas Gregor9bd6db52010-01-26 19:19:08 +00001239void clang_getInstantiationLocation(CXSourceLocation location,
1240 CXFile *file,
1241 unsigned *line,
1242 unsigned *column,
1243 unsigned *offset) {
Douglas Gregor4f46e782010-01-19 21:36:55 +00001244 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1245
Daniel Dunbarc4b4d392010-02-14 01:47:36 +00001246 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor9bd6db52010-01-26 19:19:08 +00001247 if (file)
1248 *file = 0;
1249 if (line)
1250 *line = 0;
1251 if (column)
1252 *column = 0;
1253 if (offset)
1254 *offset = 0;
1255 return;
1256 }
1257
Daniel Dunbarc4b4d392010-02-14 01:47:36 +00001258 const SourceManager &SM =
1259 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001260 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001261
1262 if (file)
1263 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1264 if (line)
1265 *line = SM.getInstantiationLineNumber(InstLoc);
1266 if (column)
1267 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregor47751d62010-01-26 03:07:15 +00001268 if (offset)
Douglas Gregor9bd6db52010-01-26 19:19:08 +00001269 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregor47751d62010-01-26 03:07:15 +00001270}
1271
Douglas Gregor4f46e782010-01-19 21:36:55 +00001272CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf441baf2010-02-17 00:41:40 +00001273 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001274 range.begin_int_data };
Douglas Gregor4f46e782010-01-19 21:36:55 +00001275 return Result;
1276}
1277
1278CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarc4b4d392010-02-14 01:47:36 +00001279 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001280 range.end_int_data };
Douglas Gregor4f46e782010-01-19 21:36:55 +00001281 return Result;
1282}
1283
Douglas Gregor816fd362010-01-22 21:44:22 +00001284} // end: extern "C"
1285
Douglas Gregor4f46e782010-01-19 21:36:55 +00001286//===----------------------------------------------------------------------===//
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001287// CXFile Operations.
1288//===----------------------------------------------------------------------===//
1289
1290extern "C" {
Ted Kremenekc560b682010-02-17 00:41:20 +00001291CXString clang_getFileName(CXFile SFile) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001292 if (!SFile)
Ted Kremenekc560b682010-02-17 00:41:20 +00001293 return createCXString(NULL);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001294
Steve Naroff6231f182009-10-27 14:35:18 +00001295 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenekc560b682010-02-17 00:41:20 +00001296 return createCXString(FEnt->getName());
Steve Naroff6231f182009-10-27 14:35:18 +00001297}
1298
1299time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001300 if (!SFile)
1301 return 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001302
Steve Naroff6231f182009-10-27 14:35:18 +00001303 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1304 return FEnt->getModificationTime();
Steve Naroff26760892009-09-25 21:45:39 +00001305}
Ted Kremenekf441baf2010-02-17 00:41:40 +00001306
Douglas Gregor816fd362010-01-22 21:44:22 +00001307CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1308 if (!tu)
1309 return 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001310
Douglas Gregor816fd362010-01-22 21:44:22 +00001311 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001312
Douglas Gregor816fd362010-01-22 21:44:22 +00001313 FileManager &FMgr = CXXUnit->getFileManager();
1314 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1315 return const_cast<FileEntry *>(File);
1316}
Ted Kremenekf441baf2010-02-17 00:41:40 +00001317
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001318} // end: extern "C"
Steve Naroff26760892009-09-25 21:45:39 +00001319
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001320//===----------------------------------------------------------------------===//
1321// CXCursor Operations.
1322//===----------------------------------------------------------------------===//
1323
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001324static Decl *getDeclFromExpr(Stmt *E) {
1325 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1326 return RefExpr->getDecl();
1327 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1328 return ME->getMemberDecl();
1329 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1330 return RE->getDecl();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001331
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001332 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1333 return getDeclFromExpr(CE->getCallee());
1334 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1335 return getDeclFromExpr(CE->getSubExpr());
1336 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1337 return OME->getMethodDecl();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001338
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001339 return 0;
1340}
1341
Daniel Dunbara4a2e5d2010-02-02 05:00:22 +00001342static SourceLocation getLocationFromExpr(Expr *E) {
1343 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1344 return /*FIXME:*/Msg->getLeftLoc();
1345 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1346 return DRE->getLocation();
1347 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1348 return Member->getMemberLoc();
1349 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1350 return Ivar->getLocation();
1351 return E->getLocStart();
1352}
1353
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001354extern "C" {
Ted Kremenekf441baf2010-02-17 00:41:40 +00001355
1356unsigned clang_visitChildren(CXCursor parent,
Douglas Gregor71f3d942010-01-20 20:59:29 +00001357 CXCursorVisitor visitor,
1358 CXClientData client_data) {
Douglas Gregorfed36b12010-01-20 23:57:43 +00001359 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregor71f3d942010-01-20 20:59:29 +00001360
1361 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001362
Douglas Gregor71f3d942010-01-20 20:59:29 +00001363 // Set the PCHLevel to filter out unwanted decls if requested.
1364 if (CXXUnit->getOnlyLocalDecls()) {
1365 PCHLevel = 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001366
Douglas Gregor71f3d942010-01-20 20:59:29 +00001367 // If the main input was an AST, bump the level.
1368 if (CXXUnit->isMainFileAST())
1369 ++PCHLevel;
1370 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001371
Douglas Gregorfed36b12010-01-20 23:57:43 +00001372 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregor71f3d942010-01-20 20:59:29 +00001373 return CursorVis.VisitChildren(parent);
1374}
1375
Douglas Gregordd969c82010-01-20 21:45:58 +00001376static CXString getDeclSpelling(Decl *D) {
1377 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1378 if (!ND)
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001379 return createCXString("");
Ted Kremenekf441baf2010-02-17 00:41:40 +00001380
Douglas Gregordd969c82010-01-20 21:45:58 +00001381 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001382 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001383
Douglas Gregordd969c82010-01-20 21:45:58 +00001384 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1385 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1386 // and returns different names. NamedDecl returns the class name and
1387 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001388 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001389
Douglas Gregordd969c82010-01-20 21:45:58 +00001390 if (ND->getIdentifier())
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001391 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001392
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001393 return createCXString("");
Douglas Gregordd969c82010-01-20 21:45:58 +00001394}
Ted Kremenekf441baf2010-02-17 00:41:40 +00001395
Daniel Dunbar079203f2009-12-01 03:14:51 +00001396CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregord2fc7272010-01-20 00:23:15 +00001397 if (clang_isTranslationUnit(C.kind))
Douglas Gregorfed36b12010-01-20 23:57:43 +00001398 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregord2fc7272010-01-20 00:23:15 +00001399
Steve Naroff80a766b2009-09-02 18:26:48 +00001400 if (clang_isReference(C.kind)) {
1401 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001402 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor6c8959b2010-01-16 14:00:32 +00001403 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001404 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001405 }
1406 case CXCursor_ObjCClassRef: {
Douglas Gregor46d66142010-01-16 17:14:40 +00001407 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001408 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001409 }
1410 case CXCursor_ObjCProtocolRef: {
Douglas Gregoref6eb842010-01-16 15:44:18 +00001411 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001412 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001413 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001414 }
Douglas Gregor93f89952010-01-21 16:28:34 +00001415 case CXCursor_TypeRef: {
1416 TypeDecl *Type = getCursorTypeRef(C).first;
1417 assert(Type && "Missing type decl");
1418
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001419 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1420 getAsString());
Douglas Gregor93f89952010-01-21 16:28:34 +00001421 }
1422
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001423 default:
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001424 return createCXString("<not implemented>");
Steve Naroff80a766b2009-09-02 18:26:48 +00001425 }
1426 }
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001427
1428 if (clang_isExpression(C.kind)) {
1429 Decl *D = getDeclFromExpr(getCursorExpr(C));
1430 if (D)
Douglas Gregordd969c82010-01-20 21:45:58 +00001431 return getDeclSpelling(D);
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001432 return createCXString("");
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001433 }
1434
Douglas Gregor5bce76c2010-01-25 16:56:17 +00001435 if (clang_isDeclaration(C.kind))
1436 return getDeclSpelling(getCursorDecl(C));
Ted Kremenek29004672010-02-17 00:41:32 +00001437
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00001438 return createCXString("");
Steve Naroff80a766b2009-09-02 18:26:48 +00001439}
1440
Ted Kremenek29004672010-02-17 00:41:32 +00001441CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff1054e602009-08-31 00:59:03 +00001442 switch (Kind) {
Ted Kremenek29004672010-02-17 00:41:32 +00001443 case CXCursor_FunctionDecl:
1444 return createCXString("FunctionDecl");
1445 case CXCursor_TypedefDecl:
1446 return createCXString("TypedefDecl");
1447 case CXCursor_EnumDecl:
1448 return createCXString("EnumDecl");
1449 case CXCursor_EnumConstantDecl:
1450 return createCXString("EnumConstantDecl");
1451 case CXCursor_StructDecl:
1452 return createCXString("StructDecl");
1453 case CXCursor_UnionDecl:
1454 return createCXString("UnionDecl");
1455 case CXCursor_ClassDecl:
1456 return createCXString("ClassDecl");
1457 case CXCursor_FieldDecl:
1458 return createCXString("FieldDecl");
1459 case CXCursor_VarDecl:
1460 return createCXString("VarDecl");
1461 case CXCursor_ParmDecl:
1462 return createCXString("ParmDecl");
1463 case CXCursor_ObjCInterfaceDecl:
1464 return createCXString("ObjCInterfaceDecl");
1465 case CXCursor_ObjCCategoryDecl:
1466 return createCXString("ObjCCategoryDecl");
1467 case CXCursor_ObjCProtocolDecl:
1468 return createCXString("ObjCProtocolDecl");
1469 case CXCursor_ObjCPropertyDecl:
1470 return createCXString("ObjCPropertyDecl");
1471 case CXCursor_ObjCIvarDecl:
1472 return createCXString("ObjCIvarDecl");
1473 case CXCursor_ObjCInstanceMethodDecl:
1474 return createCXString("ObjCInstanceMethodDecl");
1475 case CXCursor_ObjCClassMethodDecl:
1476 return createCXString("ObjCClassMethodDecl");
1477 case CXCursor_ObjCImplementationDecl:
1478 return createCXString("ObjCImplementationDecl");
1479 case CXCursor_ObjCCategoryImplDecl:
1480 return createCXString("ObjCCategoryImplDecl");
1481 case CXCursor_UnexposedDecl:
1482 return createCXString("UnexposedDecl");
1483 case CXCursor_ObjCSuperClassRef:
1484 return createCXString("ObjCSuperClassRef");
1485 case CXCursor_ObjCProtocolRef:
1486 return createCXString("ObjCProtocolRef");
1487 case CXCursor_ObjCClassRef:
1488 return createCXString("ObjCClassRef");
1489 case CXCursor_TypeRef:
1490 return createCXString("TypeRef");
1491 case CXCursor_UnexposedExpr:
1492 return createCXString("UnexposedExpr");
1493 case CXCursor_DeclRefExpr:
1494 return createCXString("DeclRefExpr");
1495 case CXCursor_MemberRefExpr:
1496 return createCXString("MemberRefExpr");
1497 case CXCursor_CallExpr:
1498 return createCXString("CallExpr");
1499 case CXCursor_ObjCMessageExpr:
1500 return createCXString("ObjCMessageExpr");
1501 case CXCursor_UnexposedStmt:
1502 return createCXString("UnexposedStmt");
1503 case CXCursor_InvalidFile:
1504 return createCXString("InvalidFile");
1505 case CXCursor_NoDeclFound:
1506 return createCXString("NoDeclFound");
1507 case CXCursor_NotImplemented:
1508 return createCXString("NotImplemented");
1509 case CXCursor_TranslationUnit:
1510 return createCXString("TranslationUnit");
Ted Kremenekbff31432010-02-18 03:09:07 +00001511 case CXCursor_UnexposedAttr:
1512 return createCXString("UnexposedAttr");
1513 case CXCursor_IBActionAttr:
1514 return createCXString("attribute(ibaction)");
1515 case CXCursor_IBOutletAttr:
1516 return createCXString("attribute(iboutlet)");
Steve Naroff1054e602009-08-31 00:59:03 +00001517 }
Ted Kremenek29004672010-02-17 00:41:32 +00001518
Ted Kremenek4ba52632010-01-16 02:02:09 +00001519 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremenek29004672010-02-17 00:41:32 +00001520 return createCXString(NULL);
Steve Naroffd5e8e862009-08-27 19:51:58 +00001521}
Steve Naroff1054e602009-08-31 00:59:03 +00001522
Ted Kremenek29004672010-02-17 00:41:32 +00001523enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1524 CXCursor parent,
Douglas Gregor562c1f92010-01-22 19:49:59 +00001525 CXClientData client_data) {
1526 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1527 *BestCursor = cursor;
1528 return CXChildVisit_Recurse;
1529}
Ted Kremenek29004672010-02-17 00:41:32 +00001530
Douglas Gregor816fd362010-01-22 21:44:22 +00001531CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1532 if (!TU)
Ted Kremeneke34cbde2010-01-14 01:51:23 +00001533 return clang_getNullCursor();
Ted Kremenek29004672010-02-17 00:41:32 +00001534
Douglas Gregor816fd362010-01-22 21:44:22 +00001535 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1536
Douglas Gregor0c7c2f82010-03-05 21:16:25 +00001537 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
1538
Ted Kremenek97a45372010-01-25 22:34:44 +00001539 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor562c1f92010-01-22 19:49:59 +00001540 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1541 if (SLoc.isValid()) {
Daniel Dunbar02968e52010-02-14 10:02:57 +00001542 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremenek29004672010-02-17 00:41:32 +00001543
Douglas Gregor562c1f92010-01-22 19:49:59 +00001544 // FIXME: Would be great to have a "hint" cursor, then walk from that
1545 // hint cursor upward until we find a cursor whose source range encloses
1546 // the region of interest, rather than starting from the translation unit.
1547 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenek29004672010-02-17 00:41:32 +00001548 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor562c1f92010-01-22 19:49:59 +00001549 Decl::MaxPCHLevel, RegionOfInterest);
1550 CursorVis.VisitChildren(Parent);
Steve Naroff54f22fb2009-09-15 20:25:34 +00001551 }
Ted Kremenek29004672010-02-17 00:41:32 +00001552 return Result;
Steve Naroffd5e8e862009-08-27 19:51:58 +00001553}
1554
Ted Kremeneke05d7802009-11-17 19:28:59 +00001555CXCursor clang_getNullCursor(void) {
Douglas Gregor58552bc2010-01-20 23:34:41 +00001556 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremeneke05d7802009-11-17 19:28:59 +00001557}
1558
1559unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +00001560 return X == Y;
Ted Kremeneke05d7802009-11-17 19:28:59 +00001561}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001562
Daniel Dunbar079203f2009-12-01 03:14:51 +00001563unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff54f22fb2009-09-15 20:25:34 +00001564 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1565}
1566
Daniel Dunbar079203f2009-12-01 03:14:51 +00001567unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff1054e602009-08-31 00:59:03 +00001568 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1569}
Steve Naroff772c1a42009-08-31 14:26:51 +00001570
Daniel Dunbar079203f2009-12-01 03:14:51 +00001571unsigned clang_isReference(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +00001572 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1573}
1574
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001575unsigned clang_isExpression(enum CXCursorKind K) {
1576 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1577}
1578
1579unsigned clang_isStatement(enum CXCursorKind K) {
1580 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1581}
1582
Douglas Gregord2fc7272010-01-20 00:23:15 +00001583unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1584 return K == CXCursor_TranslationUnit;
1585}
1586
Ted Kremenekff9021b2010-03-08 21:17:29 +00001587unsigned clang_isUnexposed(enum CXCursorKind K) {
1588 switch (K) {
1589 case CXCursor_UnexposedDecl:
1590 case CXCursor_UnexposedExpr:
1591 case CXCursor_UnexposedStmt:
1592 case CXCursor_UnexposedAttr:
1593 return true;
1594 default:
1595 return false;
1596 }
1597}
1598
Daniel Dunbar079203f2009-12-01 03:14:51 +00001599CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroffef9618b2009-09-04 15:44:05 +00001600 return C.kind;
1601}
1602
Douglas Gregor66a58812010-01-18 22:46:11 +00001603CXSourceLocation clang_getCursorLocation(CXCursor C) {
1604 if (clang_isReference(C.kind)) {
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001605 switch (C.kind) {
Ted Kremenekf441baf2010-02-17 00:41:40 +00001606 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001607 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1608 = getCursorObjCSuperClassRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001609 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001610 }
1611
Ted Kremenekf441baf2010-02-17 00:41:40 +00001612 case CXCursor_ObjCProtocolRef: {
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001613 std::pair<ObjCProtocolDecl *, SourceLocation> P
1614 = getCursorObjCProtocolRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001615 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001616 }
1617
Ted Kremenekf441baf2010-02-17 00:41:40 +00001618 case CXCursor_ObjCClassRef: {
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001619 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1620 = getCursorObjCClassRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001621 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001622 }
Douglas Gregor93f89952010-01-21 16:28:34 +00001623
Ted Kremenekf441baf2010-02-17 00:41:40 +00001624 case CXCursor_TypeRef: {
Douglas Gregor93f89952010-01-21 16:28:34 +00001625 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001626 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor93f89952010-01-21 16:28:34 +00001627 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001628
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001629 default:
1630 // FIXME: Need a way to enumerate all non-reference cases.
1631 llvm_unreachable("Missed a reference kind");
1632 }
Douglas Gregor66a58812010-01-18 22:46:11 +00001633 }
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001634
1635 if (clang_isExpression(C.kind))
Ted Kremenekf441baf2010-02-17 00:41:40 +00001636 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001637 getLocationFromExpr(getCursorExpr(C)));
1638
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001639 if (!getCursorDecl(C))
1640 return clang_getNullLocation();
Douglas Gregor66a58812010-01-18 22:46:11 +00001641
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001642 Decl *D = getCursorDecl(C);
Douglas Gregor7ecd0202010-01-18 23:41:10 +00001643 SourceLocation Loc = D->getLocation();
1644 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1645 Loc = Class->getClassLoc();
Ted Kremenek97a45372010-01-25 22:34:44 +00001646 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001647}
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001648
1649CXSourceRange clang_getCursorExtent(CXCursor C) {
1650 if (clang_isReference(C.kind)) {
1651 switch (C.kind) {
Ted Kremenekf441baf2010-02-17 00:41:40 +00001652 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001653 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1654 = getCursorObjCSuperClassRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001655 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001656 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001657
1658 case CXCursor_ObjCProtocolRef: {
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001659 std::pair<ObjCProtocolDecl *, SourceLocation> P
1660 = getCursorObjCProtocolRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001661 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001662 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001663
1664 case CXCursor_ObjCClassRef: {
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001665 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1666 = getCursorObjCClassRef(C);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001667
Ted Kremenek97a45372010-01-25 22:34:44 +00001668 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001669 }
Douglas Gregor93f89952010-01-21 16:28:34 +00001670
Ted Kremenekf441baf2010-02-17 00:41:40 +00001671 case CXCursor_TypeRef: {
Douglas Gregor93f89952010-01-21 16:28:34 +00001672 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001673 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor93f89952010-01-21 16:28:34 +00001674 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001675
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001676 default:
1677 // FIXME: Need a way to enumerate all non-reference cases.
1678 llvm_unreachable("Missed a reference kind");
1679 }
1680 }
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001681
1682 if (clang_isExpression(C.kind))
Ted Kremenekf441baf2010-02-17 00:41:40 +00001683 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001684 getCursorExpr(C)->getSourceRange());
Douglas Gregor562c1f92010-01-22 19:49:59 +00001685
1686 if (clang_isStatement(C.kind))
Ted Kremenekf441baf2010-02-17 00:41:40 +00001687 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor562c1f92010-01-22 19:49:59 +00001688 getCursorStmt(C)->getSourceRange());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001689
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001690 if (!getCursorDecl(C))
1691 return clang_getNullRange();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001692
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001693 Decl *D = getCursorDecl(C);
Ted Kremenek97a45372010-01-25 22:34:44 +00001694 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001695}
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001696
1697CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorfed36b12010-01-20 23:57:43 +00001698 if (clang_isInvalid(C.kind))
1699 return clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001700
Douglas Gregorfed36b12010-01-20 23:57:43 +00001701 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001702 if (clang_isDeclaration(C.kind))
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001703 return C;
Ted Kremenekf441baf2010-02-17 00:41:40 +00001704
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001705 if (clang_isExpression(C.kind)) {
1706 Decl *D = getDeclFromExpr(getCursorExpr(C));
1707 if (D)
Douglas Gregorfed36b12010-01-20 23:57:43 +00001708 return MakeCXCursor(D, CXXUnit);
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001709 return clang_getNullCursor();
1710 }
1711
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001712 if (!clang_isReference(C.kind))
1713 return clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001714
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001715 switch (C.kind) {
1716 case CXCursor_ObjCSuperClassRef:
Douglas Gregorfed36b12010-01-20 23:57:43 +00001717 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001718
1719 case CXCursor_ObjCProtocolRef: {
Douglas Gregorfed36b12010-01-20 23:57:43 +00001720 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001721
1722 case CXCursor_ObjCClassRef:
Douglas Gregorfed36b12010-01-20 23:57:43 +00001723 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor93f89952010-01-21 16:28:34 +00001724
Ted Kremenekf441baf2010-02-17 00:41:40 +00001725 case CXCursor_TypeRef:
Douglas Gregor93f89952010-01-21 16:28:34 +00001726 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001727
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001728 default:
1729 // We would prefer to enumerate all non-reference cursor kinds here.
1730 llvm_unreachable("Unhandled reference cursor kind");
1731 break;
1732 }
1733 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001734
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001735 return clang_getNullCursor();
1736}
1737
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001738CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorfed36b12010-01-20 23:57:43 +00001739 if (clang_isInvalid(C.kind))
1740 return clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001741
Douglas Gregorfed36b12010-01-20 23:57:43 +00001742 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001743
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001744 bool WasReference = false;
Douglas Gregor8f40bbee2010-01-19 23:20:36 +00001745 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001746 C = clang_getCursorReferenced(C);
1747 WasReference = true;
1748 }
1749
1750 if (!clang_isDeclaration(C.kind))
1751 return clang_getNullCursor();
1752
1753 Decl *D = getCursorDecl(C);
1754 if (!D)
1755 return clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001756
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001757 switch (D->getKind()) {
1758 // Declaration kinds that don't really separate the notions of
1759 // declaration and definition.
1760 case Decl::Namespace:
1761 case Decl::Typedef:
1762 case Decl::TemplateTypeParm:
1763 case Decl::EnumConstant:
1764 case Decl::Field:
1765 case Decl::ObjCIvar:
1766 case Decl::ObjCAtDefsField:
1767 case Decl::ImplicitParam:
1768 case Decl::ParmVar:
1769 case Decl::NonTypeTemplateParm:
1770 case Decl::TemplateTemplateParm:
1771 case Decl::ObjCCategoryImpl:
1772 case Decl::ObjCImplementation:
1773 case Decl::LinkageSpec:
1774 case Decl::ObjCPropertyImpl:
1775 case Decl::FileScopeAsm:
1776 case Decl::StaticAssert:
1777 case Decl::Block:
1778 return C;
1779
1780 // Declaration kinds that don't make any sense here, but are
1781 // nonetheless harmless.
1782 case Decl::TranslationUnit:
1783 case Decl::Template:
1784 case Decl::ObjCContainer:
1785 break;
1786
1787 // Declaration kinds for which the definition is not resolvable.
1788 case Decl::UnresolvedUsingTypename:
1789 case Decl::UnresolvedUsingValue:
1790 break;
1791
1792 case Decl::UsingDirective:
Douglas Gregorfed36b12010-01-20 23:57:43 +00001793 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1794 CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001795
1796 case Decl::NamespaceAlias:
Douglas Gregorfed36b12010-01-20 23:57:43 +00001797 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001798
1799 case Decl::Enum:
1800 case Decl::Record:
1801 case Decl::CXXRecord:
1802 case Decl::ClassTemplateSpecialization:
1803 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001804 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001805 return MakeCXCursor(Def, CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001806 return clang_getNullCursor();
1807
1808 case Decl::Function:
1809 case Decl::CXXMethod:
1810 case Decl::CXXConstructor:
1811 case Decl::CXXDestructor:
1812 case Decl::CXXConversion: {
1813 const FunctionDecl *Def = 0;
1814 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorfed36b12010-01-20 23:57:43 +00001815 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001816 return clang_getNullCursor();
1817 }
1818
1819 case Decl::Var: {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001820 // Ask the variable if it has a definition.
1821 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1822 return MakeCXCursor(Def, CXXUnit);
1823 return clang_getNullCursor();
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001824 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001825
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001826 case Decl::FunctionTemplate: {
1827 const FunctionDecl *Def = 0;
1828 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorfed36b12010-01-20 23:57:43 +00001829 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001830 return clang_getNullCursor();
1831 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00001832
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001833 case Decl::ClassTemplate: {
1834 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001835 ->getDefinition())
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001836 return MakeCXCursor(
Ted Kremenekf441baf2010-02-17 00:41:40 +00001837 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorfed36b12010-01-20 23:57:43 +00001838 CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001839 return clang_getNullCursor();
1840 }
1841
1842 case Decl::Using: {
1843 UsingDecl *Using = cast<UsingDecl>(D);
1844 CXCursor Def = clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001845 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1846 SEnd = Using->shadow_end();
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001847 S != SEnd; ++S) {
1848 if (Def != clang_getNullCursor()) {
1849 // FIXME: We have no way to return multiple results.
1850 return clang_getNullCursor();
1851 }
1852
Ted Kremenekf441baf2010-02-17 00:41:40 +00001853 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorfed36b12010-01-20 23:57:43 +00001854 CXXUnit));
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001855 }
1856
1857 return Def;
1858 }
1859
1860 case Decl::UsingShadow:
1861 return clang_getCursorDefinition(
Ted Kremenekf441baf2010-02-17 00:41:40 +00001862 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorfed36b12010-01-20 23:57:43 +00001863 CXXUnit));
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001864
1865 case Decl::ObjCMethod: {
1866 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1867 if (Method->isThisDeclarationADefinition())
1868 return C;
1869
1870 // Dig out the method definition in the associated
1871 // @implementation, if we have it.
1872 // FIXME: The ASTs should make finding the definition easier.
1873 if (ObjCInterfaceDecl *Class
1874 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1875 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1876 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1877 Method->isInstanceMethod()))
1878 if (Def->isThisDeclarationADefinition())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001879 return MakeCXCursor(Def, CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001880
1881 return clang_getNullCursor();
1882 }
1883
1884 case Decl::ObjCCategory:
1885 if (ObjCCategoryImplDecl *Impl
1886 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001887 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001888 return clang_getNullCursor();
1889
1890 case Decl::ObjCProtocol:
1891 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1892 return C;
1893 return clang_getNullCursor();
1894
1895 case Decl::ObjCInterface:
1896 // There are two notions of a "definition" for an Objective-C
1897 // class: the interface and its implementation. When we resolved a
1898 // reference to an Objective-C class, produce the @interface as
1899 // the definition; when we were provided with the interface,
1900 // produce the @implementation as the definition.
1901 if (WasReference) {
1902 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1903 return C;
1904 } else if (ObjCImplementationDecl *Impl
1905 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001906 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001907 return clang_getNullCursor();
Ted Kremenekf441baf2010-02-17 00:41:40 +00001908
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001909 case Decl::ObjCProperty:
1910 // FIXME: We don't really know where to find the
1911 // ObjCPropertyImplDecls that implement this property.
1912 return clang_getNullCursor();
1913
1914 case Decl::ObjCCompatibleAlias:
1915 if (ObjCInterfaceDecl *Class
1916 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1917 if (!Class->isForwardDecl())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001918 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf441baf2010-02-17 00:41:40 +00001919
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001920 return clang_getNullCursor();
1921
1922 case Decl::ObjCForwardProtocol: {
1923 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1924 if (Forward->protocol_size() == 1)
1925 return clang_getCursorDefinition(
Ted Kremenekf441baf2010-02-17 00:41:40 +00001926 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorfed36b12010-01-20 23:57:43 +00001927 CXXUnit));
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001928
1929 // FIXME: Cannot return multiple definitions.
1930 return clang_getNullCursor();
1931 }
1932
1933 case Decl::ObjCClass: {
1934 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1935 if (Class->size() == 1) {
1936 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1937 if (!IFace->isForwardDecl())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001938 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001939 return clang_getNullCursor();
1940 }
1941
1942 // FIXME: Cannot return multiple definitions.
1943 return clang_getNullCursor();
1944 }
1945
1946 case Decl::Friend:
1947 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001948 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001949 return clang_getNullCursor();
1950
1951 case Decl::FriendTemplate:
1952 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorfed36b12010-01-20 23:57:43 +00001953 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001954 return clang_getNullCursor();
1955 }
1956
1957 return clang_getNullCursor();
1958}
1959
1960unsigned clang_isCursorDefinition(CXCursor C) {
1961 if (!clang_isDeclaration(C.kind))
1962 return 0;
1963
1964 return clang_getCursorDefinition(C) == C;
1965}
1966
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001967void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff76b8f132009-09-23 17:52:52 +00001968 const char **startBuf,
1969 const char **endBuf,
1970 unsigned *startLine,
1971 unsigned *startColumn,
1972 unsigned *endLine,
Daniel Dunbar079203f2009-12-01 03:14:51 +00001973 unsigned *endColumn) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +00001974 assert(getCursorDecl(C) && "CXCursor has null decl");
1975 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff76b8f132009-09-23 17:52:52 +00001976 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1977 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf441baf2010-02-17 00:41:40 +00001978
Steve Naroff76b8f132009-09-23 17:52:52 +00001979 SourceManager &SM = FD->getASTContext().getSourceManager();
1980 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1981 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1982 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1983 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1984 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1985 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1986}
Ted Kremenekf441baf2010-02-17 00:41:40 +00001987
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001988void clang_enableStackTraces(void) {
1989 llvm::sys::PrintStackTraceOnErrorSignal();
1990}
1991
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001992} // end: extern "C"
Steve Naroff76b8f132009-09-23 17:52:52 +00001993
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001994//===----------------------------------------------------------------------===//
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001995// Token-based Operations.
1996//===----------------------------------------------------------------------===//
1997
1998/* CXToken layout:
1999 * int_data[0]: a CXTokenKind
2000 * int_data[1]: starting token location
2001 * int_data[2]: token length
2002 * int_data[3]: reserved
Ted Kremenekf441baf2010-02-17 00:41:40 +00002003 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002004 * otherwise unused.
2005 */
2006extern "C" {
2007
2008CXTokenKind clang_getTokenKind(CXToken CXTok) {
2009 return static_cast<CXTokenKind>(CXTok.int_data[0]);
2010}
2011
2012CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
2013 switch (clang_getTokenKind(CXTok)) {
2014 case CXToken_Identifier:
2015 case CXToken_Keyword:
2016 // We know we have an IdentifierInfo*, so use that.
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002017 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
2018 ->getNameStart());
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002019
2020 case CXToken_Literal: {
2021 // We have stashed the starting pointer in the ptr_data field. Use it.
2022 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002023 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002024 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00002025
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002026 case CXToken_Punctuation:
2027 case CXToken_Comment:
2028 break;
2029 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00002030
2031 // We have to find the starting buffer pointer the hard way, by
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002032 // deconstructing the source location.
2033 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2034 if (!CXXUnit)
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002035 return createCXString("");
Ted Kremenekf441baf2010-02-17 00:41:40 +00002036
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002037 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2038 std::pair<FileID, unsigned> LocInfo
2039 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
2040 std::pair<const char *,const char *> Buffer
2041 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
2042
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002043 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
2044 CXTok.int_data[2]));
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002045}
Ted Kremenekf441baf2010-02-17 00:41:40 +00002046
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002047CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2048 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2049 if (!CXXUnit)
2050 return clang_getNullLocation();
Ted Kremenekf441baf2010-02-17 00:41:40 +00002051
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002052 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2053 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2054}
2055
2056CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2057 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor4f9c3762010-01-28 00:27:43 +00002058 if (!CXXUnit)
2059 return clang_getNullRange();
Ted Kremenekf441baf2010-02-17 00:41:40 +00002060
2061 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002062 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2063}
Ted Kremenekf441baf2010-02-17 00:41:40 +00002064
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002065void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2066 CXToken **Tokens, unsigned *NumTokens) {
2067 if (Tokens)
2068 *Tokens = 0;
2069 if (NumTokens)
2070 *NumTokens = 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002071
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002072 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2073 if (!CXXUnit || !Tokens || !NumTokens)
2074 return;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002075
Douglas Gregor0c7c2f82010-03-05 21:16:25 +00002076 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2077
Daniel Dunbar80daf532010-02-14 08:31:57 +00002078 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002079 if (R.isInvalid())
2080 return;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002081
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002082 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2083 std::pair<FileID, unsigned> BeginLocInfo
2084 = SourceMgr.getDecomposedLoc(R.getBegin());
2085 std::pair<FileID, unsigned> EndLocInfo
2086 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf441baf2010-02-17 00:41:40 +00002087
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002088 // Cannot tokenize across files.
2089 if (BeginLocInfo.first != EndLocInfo.first)
2090 return;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002091
2092 // Create a lexer
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002093 std::pair<const char *,const char *> Buffer
2094 = SourceMgr.getBufferData(BeginLocInfo.first);
2095 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2096 CXXUnit->getASTContext().getLangOptions(),
2097 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2098 Lex.SetCommentRetentionState(true);
Ted Kremenekf441baf2010-02-17 00:41:40 +00002099
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002100 // Lex tokens until we hit the end of the range.
2101 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2102 llvm::SmallVector<CXToken, 32> CXTokens;
2103 Token Tok;
2104 do {
2105 // Lex the next token
2106 Lex.LexFromRawLexer(Tok);
2107 if (Tok.is(tok::eof))
2108 break;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002109
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002110 // Initialize the CXToken.
2111 CXToken CXTok;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002112
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002113 // - Common fields
2114 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2115 CXTok.int_data[2] = Tok.getLength();
2116 CXTok.int_data[3] = 0;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002117
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002118 // - Kind-specific fields
2119 if (Tok.isLiteral()) {
2120 CXTok.int_data[0] = CXToken_Literal;
2121 CXTok.ptr_data = (void *)Tok.getLiteralData();
2122 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf441baf2010-02-17 00:41:40 +00002123 // Lookup the identifier to determine whether we have a
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002124 std::pair<FileID, unsigned> LocInfo
2125 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf441baf2010-02-17 00:41:40 +00002126 const char *StartPos
2127 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002128 LocInfo.second;
2129 IdentifierInfo *II
2130 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2131 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2132 CXToken_Identifier
2133 : CXToken_Keyword;
2134 CXTok.ptr_data = II;
2135 } else if (Tok.is(tok::comment)) {
2136 CXTok.int_data[0] = CXToken_Comment;
2137 CXTok.ptr_data = 0;
2138 } else {
2139 CXTok.int_data[0] = CXToken_Punctuation;
2140 CXTok.ptr_data = 0;
2141 }
2142 CXTokens.push_back(CXTok);
2143 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf441baf2010-02-17 00:41:40 +00002144
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002145 if (CXTokens.empty())
2146 return;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002147
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002148 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2149 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2150 *NumTokens = CXTokens.size();
2151}
Douglas Gregor61656112010-01-26 18:31:56 +00002152
2153typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2154
Ted Kremenekf441baf2010-02-17 00:41:40 +00002155enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2156 CXCursor parent,
Douglas Gregor61656112010-01-26 18:31:56 +00002157 CXClientData client_data) {
2158 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2159
2160 // We only annotate the locations of declarations, simple
2161 // references, and expressions which directly reference something.
2162 CXCursorKind Kind = clang_getCursorKind(cursor);
2163 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2164 // Okay: We can annotate the location of this declaration with the
2165 // declaration or reference
2166 } else if (clang_isExpression(cursor.kind)) {
2167 if (Kind != CXCursor_DeclRefExpr &&
2168 Kind != CXCursor_MemberRefExpr &&
2169 Kind != CXCursor_ObjCMessageExpr)
2170 return CXChildVisit_Recurse;
2171
2172 CXCursor Referenced = clang_getCursorReferenced(cursor);
2173 if (Referenced == cursor || Referenced == clang_getNullCursor())
2174 return CXChildVisit_Recurse;
Ted Kremenekf441baf2010-02-17 00:41:40 +00002175
Douglas Gregor61656112010-01-26 18:31:56 +00002176 // Okay: we can annotate the location of this expression
2177 } else {
2178 // Nothing to annotate
2179 return CXChildVisit_Recurse;
2180 }
Ted Kremenekf441baf2010-02-17 00:41:40 +00002181
Douglas Gregor61656112010-01-26 18:31:56 +00002182 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2183 (*Data)[Loc.int_data] = cursor;
2184 return CXChildVisit_Recurse;
2185}
2186
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002187void clang_annotateTokens(CXTranslationUnit TU,
2188 CXToken *Tokens, unsigned NumTokens,
2189 CXCursor *Cursors) {
Douglas Gregor61656112010-01-26 18:31:56 +00002190 if (NumTokens == 0)
2191 return;
2192
2193 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002194 for (unsigned I = 0; I != NumTokens; ++I)
2195 Cursors[I] = clang_getNullCursor();
Douglas Gregor61656112010-01-26 18:31:56 +00002196
2197 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2198 if (!CXXUnit || !Tokens)
2199 return;
2200
Douglas Gregor0c7c2f82010-03-05 21:16:25 +00002201 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2202
Ted Kremenekf441baf2010-02-17 00:41:40 +00002203 // Annotate all of the source locations in the region of interest that map
Douglas Gregor61656112010-01-26 18:31:56 +00002204 SourceRange RegionOfInterest;
2205 RegionOfInterest.setBegin(
2206 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2207 SourceLocation End
Ted Kremenekf441baf2010-02-17 00:41:40 +00002208 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor61656112010-01-26 18:31:56 +00002209 Tokens[NumTokens - 1]));
Daniel Dunbar02968e52010-02-14 10:02:57 +00002210 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor61656112010-01-26 18:31:56 +00002211 // FIXME: Would be great to have a "hint" cursor, then walk from that
2212 // hint cursor upward until we find a cursor whose source range encloses
2213 // the region of interest, rather than starting from the translation unit.
2214 AnnotateTokensData Annotated;
2215 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf441baf2010-02-17 00:41:40 +00002216 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor61656112010-01-26 18:31:56 +00002217 Decl::MaxPCHLevel, RegionOfInterest);
2218 AnnotateVis.VisitChildren(Parent);
2219
2220 for (unsigned I = 0; I != NumTokens; ++I) {
2221 // Determine whether we saw a cursor at this token's location.
2222 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2223 if (Pos == Annotated.end())
2224 continue;
2225
2226 Cursors[I] = Pos->second;
2227 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002228}
2229
Ted Kremenekf441baf2010-02-17 00:41:40 +00002230void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002231 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor61656112010-01-26 18:31:56 +00002232 free(Tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002233}
Ted Kremenekf441baf2010-02-17 00:41:40 +00002234
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002235} // end: extern "C"
2236
2237//===----------------------------------------------------------------------===//
Ted Kremenekfb4961d2010-03-03 06:36:57 +00002238// Operations for querying linkage of a cursor.
2239//===----------------------------------------------------------------------===//
2240
2241extern "C" {
2242CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
2243 Decl *D = cxcursor::getCursorDecl(cursor);
2244 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2245 switch (ND->getLinkage()) {
2246 case NoLinkage: return CXLinkage_NoLinkage;
2247 case InternalLinkage: return CXLinkage_Internal;
2248 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2249 case ExternalLinkage: return CXLinkage_External;
2250 };
2251
2252 return CXLinkage_Invalid;
2253}
2254} // end: extern "C"
2255
2256//===----------------------------------------------------------------------===//
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00002257// CXString Operations.
2258//===----------------------------------------------------------------------===//
2259
2260extern "C" {
2261const char *clang_getCString(CXString string) {
2262 return string.Spelling;
2263}
2264
2265void clang_disposeString(CXString string) {
2266 if (string.MustFreeString && string.Spelling)
2267 free((void*)string.Spelling);
2268}
Ted Kremenekc0f3f722010-01-22 22:44:15 +00002269
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00002270} // end: extern "C"
Ted Kremenekc0f3f722010-01-22 22:44:15 +00002271
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002272namespace clang { namespace cxstring {
2273CXString createCXString(const char *String, bool DupString){
2274 CXString Str;
2275 if (DupString) {
2276 Str.Spelling = strdup(String);
2277 Str.MustFreeString = 1;
2278 } else {
2279 Str.Spelling = String;
2280 Str.MustFreeString = 0;
2281 }
2282 return Str;
2283}
2284
2285CXString createCXString(llvm::StringRef String, bool DupString) {
2286 CXString Result;
2287 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2288 char *Spelling = (char *)malloc(String.size() + 1);
2289 memmove(Spelling, String.data(), String.size());
2290 Spelling[String.size()] = 0;
2291 Result.Spelling = Spelling;
2292 Result.MustFreeString = 1;
2293 } else {
2294 Result.Spelling = String.data();
2295 Result.MustFreeString = 0;
2296 }
2297 return Result;
2298}
2299}}
2300
Ted Kremenekc0f3f722010-01-22 22:44:15 +00002301//===----------------------------------------------------------------------===//
2302// Misc. utility functions.
2303//===----------------------------------------------------------------------===//
Ted Kremenekf441baf2010-02-17 00:41:40 +00002304
Ted Kremenekc0f3f722010-01-22 22:44:15 +00002305extern "C" {
2306
Ted Kremeneka3e65702010-02-12 22:54:40 +00002307CXString clang_getClangVersion() {
Ted Kremenek5cca6eb2010-02-17 00:41:08 +00002308 return createCXString(getClangFullVersion());
Ted Kremenekc0f3f722010-01-22 22:44:15 +00002309}
2310
2311} // end: extern "C"