blob: ad3f1df105d17c019a2d826ca6f25054f4b98a81 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenekab188932010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekd2fa5662009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "CIndexer.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremenek0a90d322010-11-17 23:24:11 +000017#include "CXTranslationUnit.h"
Ted Kremeneked122732010-11-16 01:56:27 +000018#include "CXString.h"
Ted Kremenek95f33552010-08-26 01:42:22 +000019#include "CXType.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000020#include "CXSourceLocation.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000021#include "CIndexDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000022
Ted Kremenek04bb7162010-01-22 22:44:15 +000023#include "clang/Basic/Version.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000024
Steve Naroff50398192009-08-28 15:28:48 +000025#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000026#include "clang/AST/StmtVisitor.h"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000027#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000028#include "clang/Basic/Diagnostic.h"
29#include "clang/Frontend/ASTUnit.h"
30#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000031#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000032#include "clang/Lex/Lexer.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000033#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000034#include "clang/Lex/Preprocessor.h"
Douglas Gregora67e03f2010-09-09 21:42:20 +000035#include "llvm/ADT/STLExtras.h"
Ted Kremenekd8c370c2010-11-02 23:10:24 +000036#include "llvm/ADT/Optional.h"
37#include "clang/Analysis/Support/SaveAndRestore.h"
Daniel Dunbarc7df4f32010-08-18 18:43:14 +000038#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar48615ff2010-10-08 19:30:33 +000039#include "llvm/Support/PrettyStackTrace.h"
Douglas Gregor02465752009-10-16 21:24:31 +000040#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor358559d2010-10-02 22:49:11 +000041#include "llvm/Support/raw_ostream.h"
Douglas Gregor7a07fcb2010-08-09 21:00:09 +000042#include "llvm/Support/Timer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000043#include "llvm/Support/Mutex.h"
44#include "llvm/Support/Program.h"
45#include "llvm/Support/Signals.h"
46#include "llvm/Support/Threading.h"
Ted Kremenek37f1ea02010-11-15 23:11:54 +000047#include "llvm/Support/Compiler.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000048
Steve Naroff50398192009-08-28 15:28:48 +000049using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000050using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000051using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000052
Ted Kremeneka60ed472010-11-16 08:15:36 +000053static CXTranslationUnit MakeCXTranslationUnit(ASTUnit *TU) {
54 if (!TU)
55 return 0;
56 CXTranslationUnit D = new CXTranslationUnitImpl();
57 D->TUData = TU;
58 D->StringPool = createCXStringPool();
59 return D;
60}
61
Douglas Gregor33e9abd2010-01-22 19:49:59 +000062/// \brief The result of comparing two source ranges.
63enum RangeComparisonResult {
64 /// \brief Either the ranges overlap or one of the ranges is invalid.
65 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000066
Douglas Gregor33e9abd2010-01-22 19:49:59 +000067 /// \brief The first range ends before the second range starts.
68 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +000069
Douglas Gregor33e9abd2010-01-22 19:49:59 +000070 /// \brief The first range starts after the second range ends.
71 RangeAfter
72};
73
Ted Kremenekf0e23e82010-02-17 00:41:40 +000074/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +000075/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +000076static RangeComparisonResult RangeCompare(SourceManager &SM,
77 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +000078 SourceRange R2) {
79 assert(R1.isValid() && "First range is invalid?");
80 assert(R2.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000081 if (R1.getEnd() != R2.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000082 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000083 return RangeBefore;
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000084 if (R2.getEnd() != R1.getBegin() &&
Daniel Dunbard52864b2010-02-14 10:02:57 +000085 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +000086 return RangeAfter;
87 return RangeOverlap;
88}
89
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000090/// \brief Determine if a source location falls within, before, or after a
91/// a given source range.
92static RangeComparisonResult LocationCompare(SourceManager &SM,
93 SourceLocation L, SourceRange R) {
94 assert(R.isValid() && "First range is invalid?");
95 assert(L.isValid() && "Second range is invalid?");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +000096 if (L == R.getBegin() || L == R.getEnd())
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000097 return RangeOverlap;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +000098 if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
99 return RangeBefore;
100 if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
101 return RangeAfter;
102 return RangeOverlap;
103}
104
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000105/// \brief Translate a Clang source range into a CIndex source range.
106///
107/// Clang internally represents ranges where the end location points to the
108/// start of the token at the end. However, for external clients it is more
109/// useful to have a CXSourceRange be a proper half-open interval. This routine
110/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000111CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000112 const LangOptions &LangOpts,
Chris Lattner0a76aae2010-06-18 22:45:06 +0000113 const CharSourceRange &R) {
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000114 // We want the last character in this location, so we will adjust the
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000115 // location accordingly.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000116 SourceLocation EndLoc = R.getEnd();
Douglas Gregora9b06d42010-11-09 06:24:54 +0000117 if (EndLoc.isValid() && EndLoc.isMacroID())
118 EndLoc = SM.getSpellingLoc(EndLoc);
Chris Lattner0a76aae2010-06-18 22:45:06 +0000119 if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000120 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000121 EndLoc = EndLoc.getFileLocWithOffset(Length);
122 }
123
124 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
125 R.getBegin().getRawEncoding(),
126 EndLoc.getRawEncoding() };
127 return Result;
128}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000129
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000130//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000131// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000132//===----------------------------------------------------------------------===//
133
Steve Naroff89922f82009-08-31 00:59:03 +0000134namespace {
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000135
136class VisitorJob {
137public:
Ted Kremenekcdb4caf2010-11-12 21:34:12 +0000138 enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind,
Ted Kremeneke4979cc2010-11-13 00:58:18 +0000139 TypeLocVisitKind, OverloadExprPartsKind,
Ted Kremenek60608ec2010-11-17 00:50:47 +0000140 DeclRefExprPartsKind, LabelRefVisitKind,
Ted Kremenekf64d8032010-11-18 00:02:32 +0000141 ExplicitTemplateArgsVisitKind,
142 NestedNameSpecifierVisitKind,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000143 NestedNameSpecifierLocVisitKind,
Ted Kremenekcdba6592010-11-18 00:42:18 +0000144 DeclarationNameInfoVisitKind,
Douglas Gregor94d96292011-01-19 20:34:17 +0000145 MemberRefVisitKind, SizeOfPackExprPartsKind };
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000146protected:
Ted Kremenekf64d8032010-11-18 00:02:32 +0000147 void *data[3];
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000148 CXCursor parent;
149 Kind K;
Ted Kremenekf64d8032010-11-18 00:02:32 +0000150 VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0)
151 : parent(C), K(k) {
152 data[0] = d1;
153 data[1] = d2;
154 data[2] = d3;
155 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000156public:
157 Kind getKind() const { return K; }
158 const CXCursor &getParent() const { return parent; }
159 static bool classof(VisitorJob *VJ) { return true; }
160};
161
162typedef llvm::SmallVector<VisitorJob, 10> VisitorWorkList;
163
Douglas Gregorb1373d02010-01-20 20:59:29 +0000164// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000165class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Ted Kremenekcdba6592010-11-18 00:42:18 +0000166 public TypeLocVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000167{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000168 /// \brief The translation unit we are traversing.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000169 CXTranslationUnit TU;
170 ASTUnit *AU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000171
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000172 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000173 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000174
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000175 /// \brief The declaration that serves at the parent of any statement or
176 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000177 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000178
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000179 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000180 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000181
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000182 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000183 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000184
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
186 // to the visitor. Declarations with a PCH level greater than this value will
187 // be suppressed.
188 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000189
190 /// \brief When valid, a source range to which the cursor should restrict
191 /// its search.
192 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000193
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000194 // FIXME: Eventually remove. This part of a hack to support proper
195 // iteration over all Decls contained lexically within an ObjC container.
196 DeclContext::decl_iterator *DI_current;
197 DeclContext::decl_iterator DE_current;
198
Ted Kremenekd1ded662010-11-15 23:31:32 +0000199 // Cache of pre-allocated worklists for data-recursion walk of Stmts.
200 llvm::SmallVector<VisitorWorkList*, 5> WorkListFreeList;
201 llvm::SmallVector<VisitorWorkList*, 5> WorkListCache;
202
Douglas Gregorb1373d02010-01-20 20:59:29 +0000203 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000204 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000205
206 /// \brief Determine whether this particular source range comes before, comes
207 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000208 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000209 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000210 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
211
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000212 class SetParentRAII {
213 CXCursor &Parent;
214 Decl *&StmtParent;
215 CXCursor OldParent;
216
217 public:
218 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
219 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
220 {
221 Parent = NewParent;
222 if (clang_isDeclaration(Parent.kind))
223 StmtParent = getCursorDecl(Parent);
224 }
225
226 ~SetParentRAII() {
227 Parent = OldParent;
228 if (clang_isDeclaration(Parent.kind))
229 StmtParent = getCursorDecl(Parent);
230 }
231 };
232
Steve Naroff89922f82009-08-31 00:59:03 +0000233public:
Ted Kremeneka60ed472010-11-16 08:15:36 +0000234 CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor,
235 CXClientData ClientData,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000236 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000237 SourceRange RegionOfInterest = SourceRange())
Ted Kremeneka60ed472010-11-16 08:15:36 +0000238 : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)),
239 Visitor(Visitor), ClientData(ClientData),
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000240 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest),
241 DI_current(0)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000242 {
243 Parent.kind = CXCursor_NoDeclFound;
244 Parent.data[0] = 0;
245 Parent.data[1] = 0;
246 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000247 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000248 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000249
Ted Kremenekd1ded662010-11-15 23:31:32 +0000250 ~CursorVisitor() {
251 // Free the pre-allocated worklists for data-recursion.
252 for (llvm::SmallVectorImpl<VisitorWorkList*>::iterator
253 I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) {
254 delete *I;
255 }
256 }
257
Ted Kremeneka60ed472010-11-16 08:15:36 +0000258 ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); }
259 CXTranslationUnit getTU() const { return TU; }
Ted Kremenekab979612010-11-11 08:05:23 +0000260
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000261 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000262
263 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
264 getPreprocessedEntities();
265
Douglas Gregorb1373d02010-01-20 20:59:29 +0000266 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000267
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000268 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000269 bool VisitAttributes(Decl *D);
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000270 bool VisitBlockDecl(BlockDecl *B);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000271 bool VisitCXXRecordDecl(CXXRecordDecl *D);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000272 llvm::Optional<bool> shouldVisitCursor(CXCursor C);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000273 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000274 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
275 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000276 bool VisitTagDecl(TagDecl *D);
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000277 bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D);
Douglas Gregor74dbe642010-08-31 19:31:58 +0000278 bool VisitClassTemplatePartialSpecializationDecl(
279 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000280 bool VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000281 bool VisitEnumConstantDecl(EnumConstantDecl *D);
282 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
283 bool VisitFunctionDecl(FunctionDecl *ND);
284 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000285 bool VisitVarDecl(VarDecl *);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000286 bool VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000287 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +0000288 bool VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor84b51d72010-09-01 20:16:53 +0000289 bool VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000290 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
291 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
292 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
293 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
Ted Kremenek23173d72010-05-18 21:09:07 +0000294 bool VisitObjCPropertyDecl(ObjCPropertyDecl *PD);
Ted Kremenek79758f62010-02-18 22:36:18 +0000295 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
296 bool VisitObjCImplDecl(ObjCImplDecl *D);
297 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
298 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000299 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
300 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
301 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora4ffd852010-11-17 01:03:52 +0000302 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD);
Ted Kremeneka0536d82010-05-07 01:04:29 +0000303 bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
Ted Kremenek8f06e0e2010-05-06 23:38:21 +0000304 bool VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +0000305 bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000306 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor7e242562010-09-01 19:52:22 +0000307 bool VisitUsingDecl(UsingDecl *D);
308 bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
309 bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000310
Douglas Gregor01829d32010-08-31 14:41:23 +0000311 // Name visitor
312 bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000313 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range);
Douglas Gregordc355712011-02-25 00:36:19 +0000314 bool VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
Douglas Gregor01829d32010-08-31 14:41:23 +0000315
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000316 // Template visitors
317 bool VisitTemplateParameters(const TemplateParameterList *Params);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000318 bool VisitTemplateName(TemplateName Name, SourceLocation Loc);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000319 bool VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL);
320
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000321 // Type visitors
Douglas Gregor01829d32010-08-31 14:41:23 +0000322 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000323 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000324 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000325 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
326 bool VisitTagTypeLoc(TagTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000327 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000328 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
John McCallc12c5bb2010-05-15 11:32:37 +0000329 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000330 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000331 bool VisitParenTypeLoc(ParenTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000332 bool VisitPointerTypeLoc(PointerTypeLoc TL);
333 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
334 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
335 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
336 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
Douglas Gregor01829d32010-08-31 14:41:23 +0000337 bool VisitFunctionTypeLoc(FunctionTypeLoc TL, bool SkipResultType = false);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000338 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000339 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000340 // FIXME: Implement visitors here when the unimplemented TypeLocs get
341 // implemented
342 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000343 bool VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000344 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000345 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000346 bool VisitDependentTemplateSpecializationTypeLoc(
347 DependentTemplateSpecializationTypeLoc TL);
Douglas Gregor9e876872011-03-01 18:12:44 +0000348 bool VisitElaboratedTypeLoc(ElaboratedTypeLoc TL);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000349
Ted Kremenekc0e1d922010-11-11 08:05:18 +0000350 // Data-recursive visitor functions.
351 bool IsInRegionOfInterest(CXCursor C);
352 bool RunVisitorWorkList(VisitorWorkList &WL);
353 void EnqueueWorkList(VisitorWorkList &WL, Stmt *S);
Ted Kremenekcdba6592010-11-18 00:42:18 +0000354 LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S);
Steve Naroff89922f82009-08-31 00:59:03 +0000355};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000356
Ted Kremenekab188932010-01-05 19:32:54 +0000357} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000358
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000359static SourceRange getRawCursorExtent(CXCursor C);
Douglas Gregor66537982010-11-17 17:14:07 +0000360static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
361
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000362
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000363RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000364 return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000365}
366
Douglas Gregorb1373d02010-01-20 20:59:29 +0000367/// \brief Visit the given cursor and, if requested by the visitor,
368/// its children.
369///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000370/// \param Cursor the cursor to visit.
371///
372/// \param CheckRegionOfInterest if true, then the caller already checked that
373/// this cursor is within the region of interest.
374///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000375/// \returns true if the visitation should be aborted, false if it
376/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000377bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000378 if (clang_isInvalid(Cursor.kind))
379 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000380
Douglas Gregorb1373d02010-01-20 20:59:29 +0000381 if (clang_isDeclaration(Cursor.kind)) {
382 Decl *D = getCursorDecl(Cursor);
383 assert(D && "Invalid declaration cursor");
384 if (D->getPCHLevel() > MaxPCHLevel)
385 return false;
386
387 if (D->isImplicit())
388 return false;
389 }
390
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000391 // If we have a range of interest, and this cursor doesn't intersect with it,
392 // we're done.
393 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000394 SourceRange Range = getRawCursorExtent(Cursor);
Daniel Dunbarf408f322010-02-14 08:32:05 +0000395 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000396 return false;
397 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000398
Douglas Gregorb1373d02010-01-20 20:59:29 +0000399 switch (Visitor(Cursor, Parent, ClientData)) {
400 case CXChildVisit_Break:
401 return true;
402
403 case CXChildVisit_Continue:
404 return false;
405
406 case CXChildVisit_Recurse:
407 return VisitChildren(Cursor);
408 }
409
Douglas Gregorfd643772010-01-25 16:45:46 +0000410 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000411}
412
Douglas Gregor788f5a12010-03-20 00:41:21 +0000413std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
414CursorVisitor::getPreprocessedEntities() {
415 PreprocessingRecord &PPRec
Ted Kremeneka60ed472010-11-16 08:15:36 +0000416 = *AU->getPreprocessor().getPreprocessingRecord();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000417
418 bool OnlyLocalDecls
Douglas Gregor32038bb2010-12-21 19:07:48 +0000419 = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
420
421 if (OnlyLocalDecls && RegionOfInterest.isValid()) {
422 // If we would only look at local declarations but we have a region of
423 // interest, check whether that region of interest is in the main file.
424 // If not, we should traverse all declarations.
425 // FIXME: My kingdom for a proper binary search approach to finding
426 // cursors!
427 std::pair<FileID, unsigned> Location
428 = AU->getSourceManager().getDecomposedInstantiationLoc(
429 RegionOfInterest.getBegin());
430 if (Location.first != AU->getSourceManager().getMainFileID())
431 OnlyLocalDecls = false;
432 }
Douglas Gregor788f5a12010-03-20 00:41:21 +0000433
Douglas Gregor89d99802010-11-30 06:16:57 +0000434 PreprocessingRecord::iterator StartEntity, EndEntity;
435 if (OnlyLocalDecls) {
436 StartEntity = AU->pp_entity_begin();
437 EndEntity = AU->pp_entity_end();
438 } else {
439 StartEntity = PPRec.begin();
440 EndEntity = PPRec.end();
441 }
442
Douglas Gregor788f5a12010-03-20 00:41:21 +0000443 // There is no region of interest; we have to walk everything.
444 if (RegionOfInterest.isInvalid())
Douglas Gregor89d99802010-11-30 06:16:57 +0000445 return std::make_pair(StartEntity, EndEntity);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000446
447 // Find the file in which the region of interest lands.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000448 SourceManager &SM = AU->getSourceManager();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000449 std::pair<FileID, unsigned> Begin
450 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
451 std::pair<FileID, unsigned> End
452 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd());
453
454 // The region of interest spans files; we have to walk everything.
455 if (Begin.first != End.first)
Douglas Gregor89d99802010-11-30 06:16:57 +0000456 return std::make_pair(StartEntity, EndEntity);
Douglas Gregor788f5a12010-03-20 00:41:21 +0000457
458 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
Ted Kremeneka60ed472010-11-16 08:15:36 +0000459 = AU->getPreprocessedEntitiesByFile();
Douglas Gregor788f5a12010-03-20 00:41:21 +0000460 if (ByFileMap.empty()) {
461 // Build the mapping from files to sets of preprocessed entities.
Douglas Gregor89d99802010-11-30 06:16:57 +0000462 for (PreprocessingRecord::iterator E = StartEntity; E != EndEntity; ++E) {
Douglas Gregor788f5a12010-03-20 00:41:21 +0000463 std::pair<FileID, unsigned> P
464 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
Douglas Gregor89d99802010-11-30 06:16:57 +0000465
Douglas Gregor788f5a12010-03-20 00:41:21 +0000466 ByFileMap[P.first].push_back(*E);
467 }
468 }
469
470 return std::make_pair(ByFileMap[Begin.first].begin(),
471 ByFileMap[Begin.first].end());
472}
473
Douglas Gregorb1373d02010-01-20 20:59:29 +0000474/// \brief Visit the children of the given cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000475///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000476/// \returns true if the visitation should be aborted, false if it
477/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000478bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000479 if (clang_isReference(Cursor.kind)) {
480 // By definition, references have no children.
481 return false;
482 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000483
484 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000485 // done.
Ted Kremenek0f91f6a2010-05-13 00:25:00 +0000486 SetParentRAII SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000487
Douglas Gregorb1373d02010-01-20 20:59:29 +0000488 if (clang_isDeclaration(Cursor.kind)) {
489 Decl *D = getCursorDecl(Cursor);
490 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000491 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000492 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000493
Douglas Gregora59e3902010-01-21 23:27:09 +0000494 if (clang_isStatement(Cursor.kind))
495 return Visit(getCursorStmt(Cursor));
496 if (clang_isExpression(Cursor.kind))
497 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000498
Douglas Gregorb1373d02010-01-20 20:59:29 +0000499 if (clang_isTranslationUnit(Cursor.kind)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000500 CXTranslationUnit tu = getCursorTU(Cursor);
501 ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000502 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
503 RegionOfInterest.isInvalid()) {
Douglas Gregoreb8837b2010-08-03 19:06:41 +0000504 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
505 TLEnd = CXXUnit->top_level_end();
506 TL != TLEnd; ++TL) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000507 if (Visit(MakeCXCursor(*TL, tu), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000508 return true;
509 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000510 } else if (VisitDeclContext(
511 CXXUnit->getASTContext().getTranslationUnitDecl()))
512 return true;
Bob Wilson3178cb62010-03-19 03:57:57 +0000513
Douglas Gregor0396f462010-03-19 05:22:59 +0000514 // Walk the preprocessing record.
Daniel Dunbar8de30ff2010-03-20 01:11:56 +0000515 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000516 // FIXME: Once we have the ability to deserialize a preprocessing record,
517 // do so.
Douglas Gregor788f5a12010-03-20 00:41:21 +0000518 PreprocessingRecord::iterator E, EEnd;
519 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
Douglas Gregor0396f462010-03-19 05:22:59 +0000520 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000521 if (Visit(MakeMacroInstantiationCursor(MI, tu)))
Douglas Gregor0396f462010-03-19 05:22:59 +0000522 return true;
Douglas Gregor788f5a12010-03-20 00:41:21 +0000523
Douglas Gregor0396f462010-03-19 05:22:59 +0000524 continue;
525 }
526
527 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000528 if (Visit(MakeMacroDefinitionCursor(MD, tu)))
Douglas Gregor0396f462010-03-19 05:22:59 +0000529 return true;
530
531 continue;
532 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000533
534 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000535 if (Visit(MakeInclusionDirectiveCursor(ID, tu)))
Douglas Gregorecdcb882010-10-20 22:00:55 +0000536 return true;
537
538 continue;
539 }
Douglas Gregor0396f462010-03-19 05:22:59 +0000540 }
541 }
Douglas Gregor7b691f332010-01-20 21:13:59 +0000542 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000543 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000544
Douglas Gregorb1373d02010-01-20 20:59:29 +0000545 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000546 return false;
547}
548
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000549bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
John McCallfc929202010-06-04 22:33:30 +0000550 if (Visit(B->getSignatureAsWritten()->getTypeLoc()))
551 return true;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000552
Ted Kremenek664cffd2010-07-22 11:30:19 +0000553 if (Stmt *Body = B->getBody())
554 return Visit(MakeCXCursor(Body, StmtParent, TU));
555
556 return false;
Ted Kremenek1ee6cad2010-04-11 21:47:37 +0000557}
558
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000559llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
560 if (RegionOfInterest.isValid()) {
Douglas Gregor66537982010-11-17 17:14:07 +0000561 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000562 if (Range.isInvalid())
563 return llvm::Optional<bool>();
Douglas Gregor66537982010-11-17 17:14:07 +0000564
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000565 switch (CompareRegionOfInterest(Range)) {
566 case RangeBefore:
567 // This declaration comes before the region of interest; skip it.
568 return llvm::Optional<bool>();
569
570 case RangeAfter:
571 // This declaration comes after the region of interest; we're done.
572 return false;
573
574 case RangeOverlap:
575 // This declaration overlaps the region of interest; visit it.
576 break;
577 }
578 }
579 return true;
580}
581
582bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
583 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
584
585 // FIXME: Eventually remove. This part of a hack to support proper
586 // iteration over all Decls contained lexically within an ObjC container.
587 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
588 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
589
590 for ( ; I != E; ++I) {
Ted Kremenek23173d72010-05-18 21:09:07 +0000591 Decl *D = *I;
592 if (D->getLexicalDeclContext() != DC)
593 continue;
Ted Kremenek23173d72010-05-18 21:09:07 +0000594 CXCursor Cursor = MakeCXCursor(D, TU);
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000595 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
596 if (!V.hasValue())
597 continue;
598 if (!V.getValue())
599 return false;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000600 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000601 return true;
602 }
Douglas Gregorb1373d02010-01-20 20:59:29 +0000603 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000604}
605
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000606bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
607 llvm_unreachable("Translation units are visited directly by Visit()");
608 return false;
609}
610
611bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
612 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
613 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000614
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000615 return false;
616}
617
618bool CursorVisitor::VisitTagDecl(TagDecl *D) {
619 return VisitDeclContext(D);
620}
621
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000622bool CursorVisitor::VisitClassTemplateSpecializationDecl(
623 ClassTemplateSpecializationDecl *D) {
624 bool ShouldVisitBody = false;
625 switch (D->getSpecializationKind()) {
626 case TSK_Undeclared:
627 case TSK_ImplicitInstantiation:
628 // Nothing to visit
629 return false;
630
631 case TSK_ExplicitInstantiationDeclaration:
632 case TSK_ExplicitInstantiationDefinition:
633 break;
634
635 case TSK_ExplicitSpecialization:
636 ShouldVisitBody = true;
637 break;
638 }
639
640 // Visit the template arguments used in the specialization.
641 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
642 TypeLoc TL = SpecType->getTypeLoc();
643 if (TemplateSpecializationTypeLoc *TSTLoc
644 = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) {
645 for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I)
646 if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I)))
647 return true;
648 }
649 }
650
651 if (ShouldVisitBody && VisitCXXRecordDecl(D))
652 return true;
653
654 return false;
655}
656
Douglas Gregor74dbe642010-08-31 19:31:58 +0000657bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
658 ClassTemplatePartialSpecializationDecl *D) {
659 // FIXME: Visit the "outer" template parameter lists on the TagDecl
660 // before visiting these template parameters.
661 if (VisitTemplateParameters(D->getTemplateParameters()))
662 return true;
663
664 // Visit the partial specialization arguments.
665 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
666 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
667 if (VisitTemplateArgumentLoc(TemplateArgs[I]))
668 return true;
669
670 return VisitCXXRecordDecl(D);
671}
672
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000673bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregor84b51d72010-09-01 20:16:53 +0000674 // Visit the default argument.
675 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
676 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
677 if (Visit(DefArg->getTypeLoc()))
678 return true;
679
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000680 return false;
681}
682
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000683bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
684 if (Expr *Init = D->getInitExpr())
685 return Visit(MakeCXCursor(Init, StmtParent, TU));
686 return false;
687}
688
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000689bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
690 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
691 if (Visit(TSInfo->getTypeLoc()))
692 return true;
693
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000694 // Visit the nested-name-specifier, if present.
695 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
696 if (VisitNestedNameSpecifierLoc(QualifierLoc))
697 return true;
698
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000699 return false;
700}
701
Douglas Gregora67e03f2010-09-09 21:42:20 +0000702/// \brief Compare two base or member initializers based on their source order.
Sean Huntcbb67482011-01-08 20:30:50 +0000703static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
704 CXXCtorInitializer const * const *X
705 = static_cast<CXXCtorInitializer const * const *>(Xp);
706 CXXCtorInitializer const * const *Y
707 = static_cast<CXXCtorInitializer const * const *>(Yp);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000708
709 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
710 return -1;
711 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
712 return 1;
713 else
714 return 0;
715}
716
Douglas Gregorb1373d02010-01-20 20:59:29 +0000717bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregor01829d32010-08-31 14:41:23 +0000718 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
719 // Visit the function declaration's syntactic components in the order
720 // written. This requires a bit of work.
Abramo Bagnara723df242010-12-14 22:11:44 +0000721 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Douglas Gregor01829d32010-08-31 14:41:23 +0000722 FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
723
724 // If we have a function declared directly (without the use of a typedef),
725 // visit just the return type. Otherwise, just visit the function's type
726 // now.
727 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) ||
728 (!FTL && Visit(TL)))
729 return true;
730
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000731 // Visit the nested-name-specifier, if present.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000732 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
733 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +0000734 return true;
Douglas Gregor01829d32010-08-31 14:41:23 +0000735
736 // Visit the declaration name.
737 if (VisitDeclarationNameInfo(ND->getNameInfo()))
738 return true;
739
740 // FIXME: Visit explicitly-specified template arguments!
741
742 // Visit the function parameters, if we have a function type.
743 if (FTL && VisitFunctionTypeLoc(*FTL, true))
744 return true;
745
746 // FIXME: Attributes?
747 }
748
Douglas Gregora67e03f2010-09-09 21:42:20 +0000749 if (ND->isThisDeclarationADefinition()) {
750 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
751 // Find the initializers that were written in the source.
Sean Huntcbb67482011-01-08 20:30:50 +0000752 llvm::SmallVector<CXXCtorInitializer *, 4> WrittenInits;
Douglas Gregora67e03f2010-09-09 21:42:20 +0000753 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
754 IEnd = Constructor->init_end();
755 I != IEnd; ++I) {
756 if (!(*I)->isWritten())
757 continue;
758
759 WrittenInits.push_back(*I);
760 }
761
762 // Sort the initializers in source order
763 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
Sean Huntcbb67482011-01-08 20:30:50 +0000764 &CompareCXXCtorInitializers);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000765
766 // Visit the initializers in source order
767 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
Sean Huntcbb67482011-01-08 20:30:50 +0000768 CXXCtorInitializer *Init = WrittenInits[I];
Francois Pichet00eb3f92010-12-04 09:14:42 +0000769 if (Init->isAnyMemberInitializer()) {
770 if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
Douglas Gregora67e03f2010-09-09 21:42:20 +0000771 Init->getMemberLocation(), TU)))
772 return true;
773 } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) {
774 if (Visit(BaseInfo->getTypeLoc()))
775 return true;
776 }
777
778 // Visit the initializer value.
779 if (Expr *Initializer = Init->getInit())
780 if (Visit(MakeCXCursor(Initializer, ND, TU)))
781 return true;
782 }
783 }
784
785 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
786 return true;
787 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000788
Douglas Gregorb1373d02010-01-20 20:59:29 +0000789 return false;
790}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000791
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000792bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
793 if (VisitDeclaratorDecl(D))
794 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000795
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000796 if (Expr *BitWidth = D->getBitWidth())
797 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000798
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000799 return false;
800}
801
802bool CursorVisitor::VisitVarDecl(VarDecl *D) {
803 if (VisitDeclaratorDecl(D))
804 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000805
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000806 if (Expr *Init = D->getInit())
807 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000808
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000809 return false;
810}
811
Douglas Gregor84b51d72010-09-01 20:16:53 +0000812bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
813 if (VisitDeclaratorDecl(D))
814 return true;
815
816 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
817 if (Expr *DefArg = D->getDefaultArgument())
818 return Visit(MakeCXCursor(DefArg, StmtParent, TU));
819
820 return false;
821}
822
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000823bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
824 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
825 // before visiting these template parameters.
826 if (VisitTemplateParameters(D->getTemplateParameters()))
827 return true;
828
829 return VisitFunctionDecl(D->getTemplatedDecl());
830}
831
Douglas Gregor39d6f072010-08-31 19:02:00 +0000832bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
833 // FIXME: Visit the "outer" template parameter lists on the TagDecl
834 // before visiting these template parameters.
835 if (VisitTemplateParameters(D->getTemplateParameters()))
836 return true;
837
838 return VisitCXXRecordDecl(D->getTemplatedDecl());
839}
840
Douglas Gregor84b51d72010-09-01 20:16:53 +0000841bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
842 if (VisitTemplateParameters(D->getTemplateParameters()))
843 return true;
844
845 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
846 VisitTemplateArgumentLoc(D->getDefaultArgument()))
847 return true;
848
849 return false;
850}
851
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000852bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000853 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
854 if (Visit(TSInfo->getTypeLoc()))
855 return true;
856
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000857 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000858 PEnd = ND->param_end();
859 P != PEnd; ++P) {
860 if (Visit(MakeCXCursor(*P, TU)))
861 return true;
862 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000863
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000864 if (ND->isThisDeclarationADefinition() &&
865 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
866 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000867
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000868 return false;
869}
870
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000871namespace {
872 struct ContainerDeclsSort {
873 SourceManager &SM;
874 ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
875 bool operator()(Decl *A, Decl *B) {
876 SourceLocation L_A = A->getLocStart();
877 SourceLocation L_B = B->getLocStart();
878 assert(L_A.isValid() && L_B.isValid());
879 return SM.isBeforeInTranslationUnit(L_A, L_B);
880 }
881 };
882}
883
Douglas Gregora59e3902010-01-21 23:27:09 +0000884bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000885 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
886 // an @implementation can lexically contain Decls that are not properly
887 // nested in the AST. When we identify such cases, we need to retrofit
888 // this nesting here.
889 if (!DI_current)
890 return VisitDeclContext(D);
891
892 // Scan the Decls that immediately come after the container
893 // in the current DeclContext. If any fall within the
894 // container's lexical region, stash them into a vector
895 // for later processing.
896 llvm::SmallVector<Decl *, 24> DeclsInContainer;
897 SourceLocation EndLoc = D->getSourceRange().getEnd();
Ted Kremeneka60ed472010-11-16 08:15:36 +0000898 SourceManager &SM = AU->getSourceManager();
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000899 if (EndLoc.isValid()) {
900 DeclContext::decl_iterator next = *DI_current;
901 while (++next != DE_current) {
902 Decl *D_next = *next;
903 if (!D_next)
904 break;
905 SourceLocation L = D_next->getLocStart();
906 if (!L.isValid())
907 break;
908 if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
909 *DI_current = next;
910 DeclsInContainer.push_back(D_next);
911 continue;
912 }
913 break;
914 }
915 }
916
917 // The common case.
918 if (DeclsInContainer.empty())
919 return VisitDeclContext(D);
920
921 // Get all the Decls in the DeclContext, and sort them with the
922 // additional ones we've collected. Then visit them.
923 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
924 I!=E; ++I) {
925 Decl *subDecl = *I;
Ted Kremenek0582c892010-11-02 23:17:51 +0000926 if (!subDecl || subDecl->getLexicalDeclContext() != D ||
927 subDecl->getLocStart().isInvalid())
Ted Kremenekd8c370c2010-11-02 23:10:24 +0000928 continue;
929 DeclsInContainer.push_back(subDecl);
930 }
931
932 // Now sort the Decls so that they appear in lexical order.
933 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
934 ContainerDeclsSort(SM));
935
936 // Now visit the decls.
937 for (llvm::SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
938 E = DeclsInContainer.end(); I != E; ++I) {
939 CXCursor Cursor = MakeCXCursor(*I, TU);
940 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
941 if (!V.hasValue())
942 continue;
943 if (!V.getValue())
944 return false;
945 if (Visit(Cursor, true))
946 return true;
947 }
948 return false;
Douglas Gregora59e3902010-01-21 23:27:09 +0000949}
950
Douglas Gregorb1373d02010-01-20 20:59:29 +0000951bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000952 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
953 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000954 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000955
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000956 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
957 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
958 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000959 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000960 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000961
Douglas Gregora59e3902010-01-21 23:27:09 +0000962 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000963}
964
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000965bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
966 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
967 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
968 E = PID->protocol_end(); I != E; ++I, ++PL)
969 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
970 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000971
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000972 return VisitObjCContainerDecl(PID);
973}
974
Ted Kremenek23173d72010-05-18 21:09:07 +0000975bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
Douglas Gregor83cb9422010-09-09 17:09:21 +0000976 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
John McCallfc929202010-06-04 22:33:30 +0000977 return true;
978
Ted Kremenek23173d72010-05-18 21:09:07 +0000979 // FIXME: This implements a workaround with @property declarations also being
980 // installed in the DeclContext for the @interface. Eventually this code
981 // should be removed.
982 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
983 if (!CDecl || !CDecl->IsClassExtension())
984 return false;
985
986 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
987 if (!ID)
988 return false;
989
990 IdentifierInfo *PropertyId = PD->getIdentifier();
991 ObjCPropertyDecl *prevDecl =
992 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
993
994 if (!prevDecl)
995 return false;
996
997 // Visit synthesized methods since they will be skipped when visiting
998 // the @interface.
999 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +00001000 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +00001001 if (Visit(MakeCXCursor(MD, TU)))
1002 return true;
1003
1004 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
Ted Kremeneka054fb42010-09-21 20:52:59 +00001005 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
Ted Kremenek23173d72010-05-18 21:09:07 +00001006 if (Visit(MakeCXCursor(MD, TU)))
1007 return true;
1008
1009 return false;
1010}
1011
Douglas Gregorb1373d02010-01-20 20:59:29 +00001012bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001013 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +00001014 if (D->getSuperClass() &&
1015 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001016 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001017 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001018 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001019
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001020 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1021 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1022 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001023 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001024 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001025
Douglas Gregora59e3902010-01-21 23:27:09 +00001026 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001027}
1028
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001029bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1030 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001031}
1032
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001033bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001034 // 'ID' could be null when dealing with invalid code.
1035 if (ObjCInterfaceDecl *ID = D->getClassInterface())
1036 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1037 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001038
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001039 return VisitObjCImplDecl(D);
1040}
1041
1042bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1043#if 0
1044 // Issue callbacks for super class.
1045 // FIXME: No source location information!
1046 if (D->getSuperClass() &&
1047 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001048 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001049 TU)))
1050 return true;
1051#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001052
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001053 return VisitObjCImplDecl(D);
1054}
1055
1056bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
1057 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1058 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
1059 E = D->protocol_end();
1060 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001061 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +00001062 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001063
1064 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001065}
1066
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001067bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
1068 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
1069 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
1070 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001071
Douglas Gregor1ef2fc12010-01-22 00:50:27 +00001072 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +00001073}
1074
Douglas Gregora4ffd852010-11-17 01:03:52 +00001075bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1076 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1077 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1078
1079 return false;
1080}
1081
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00001082bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1083 return VisitDeclContext(D);
1084}
1085
Douglas Gregor69319002010-08-31 23:48:11 +00001086bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001087 // Visit nested-name-specifier.
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001088 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1089 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001090 return true;
Douglas Gregor69319002010-08-31 23:48:11 +00001091
1092 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1093 D->getTargetNameLoc(), TU));
1094}
1095
Douglas Gregor7e242562010-09-01 19:52:22 +00001096bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001097 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001098 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1099 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001100 return true;
Douglas Gregordc355712011-02-25 00:36:19 +00001101 }
Douglas Gregor7e242562010-09-01 19:52:22 +00001102
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001103 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1104 return true;
1105
Douglas Gregor7e242562010-09-01 19:52:22 +00001106 return VisitDeclarationNameInfo(D->getNameInfo());
1107}
1108
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001109bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001110 // Visit nested-name-specifier.
Douglas Gregordb992412011-02-25 16:33:46 +00001111 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1112 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001113 return true;
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001114
1115 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1116 D->getIdentLocation(), TU));
1117}
1118
Douglas Gregor7e242562010-09-01 19:52:22 +00001119bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001120 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001121 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1122 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001123 return true;
Douglas Gregordc355712011-02-25 00:36:19 +00001124 }
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001125
Douglas Gregor7e242562010-09-01 19:52:22 +00001126 return VisitDeclarationNameInfo(D->getNameInfo());
1127}
1128
1129bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1130 UnresolvedUsingTypenameDecl *D) {
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001131 // Visit nested-name-specifier.
Douglas Gregordc355712011-02-25 00:36:19 +00001132 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1133 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001134 return true;
1135
Douglas Gregor7e242562010-09-01 19:52:22 +00001136 return false;
1137}
1138
Douglas Gregor01829d32010-08-31 14:41:23 +00001139bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1140 switch (Name.getName().getNameKind()) {
1141 case clang::DeclarationName::Identifier:
1142 case clang::DeclarationName::CXXLiteralOperatorName:
1143 case clang::DeclarationName::CXXOperatorName:
1144 case clang::DeclarationName::CXXUsingDirective:
1145 return false;
1146
1147 case clang::DeclarationName::CXXConstructorName:
1148 case clang::DeclarationName::CXXDestructorName:
1149 case clang::DeclarationName::CXXConversionFunctionName:
1150 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1151 return Visit(TSInfo->getTypeLoc());
1152 return false;
1153
1154 case clang::DeclarationName::ObjCZeroArgSelector:
1155 case clang::DeclarationName::ObjCOneArgSelector:
1156 case clang::DeclarationName::ObjCMultiArgSelector:
1157 // FIXME: Per-identifier location info?
1158 return false;
1159 }
1160
1161 return false;
1162}
1163
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001164bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1165 SourceRange Range) {
1166 // FIXME: This whole routine is a hack to work around the lack of proper
1167 // source information in nested-name-specifiers (PR5791). Since we do have
1168 // a beginning source location, we can visit the first component of the
1169 // nested-name-specifier, if it's a single-token component.
1170 if (!NNS)
1171 return false;
1172
1173 // Get the first component in the nested-name-specifier.
1174 while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1175 NNS = Prefix;
1176
1177 switch (NNS->getKind()) {
1178 case NestedNameSpecifier::Namespace:
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001179 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1180 TU));
1181
Douglas Gregor14aba762011-02-24 02:36:08 +00001182 case NestedNameSpecifier::NamespaceAlias:
1183 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1184 Range.getBegin(), TU));
1185
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001186 case NestedNameSpecifier::TypeSpec: {
1187 // If the type has a form where we know that the beginning of the source
1188 // range matches up with a reference cursor. Visit the appropriate reference
1189 // cursor.
John McCallf4c73712011-01-19 06:33:43 +00001190 const Type *T = NNS->getAsType();
Douglas Gregorc5ade2e2010-09-02 17:35:32 +00001191 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1192 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1193 if (const TagType *Tag = dyn_cast<TagType>(T))
1194 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1195 if (const TemplateSpecializationType *TST
1196 = dyn_cast<TemplateSpecializationType>(T))
1197 return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1198 break;
1199 }
1200
1201 case NestedNameSpecifier::TypeSpecWithTemplate:
1202 case NestedNameSpecifier::Global:
1203 case NestedNameSpecifier::Identifier:
1204 break;
1205 }
1206
1207 return false;
1208}
1209
Douglas Gregordc355712011-02-25 00:36:19 +00001210bool
1211CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1212 llvm::SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1213 for (; Qualifier; Qualifier = Qualifier.getPrefix())
1214 Qualifiers.push_back(Qualifier);
1215
1216 while (!Qualifiers.empty()) {
1217 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1218 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1219 switch (NNS->getKind()) {
1220 case NestedNameSpecifier::Namespace:
1221 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001222 Q.getLocalBeginLoc(),
Douglas Gregordc355712011-02-25 00:36:19 +00001223 TU)))
1224 return true;
1225
1226 break;
1227
1228 case NestedNameSpecifier::NamespaceAlias:
1229 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001230 Q.getLocalBeginLoc(),
Douglas Gregordc355712011-02-25 00:36:19 +00001231 TU)))
1232 return true;
1233
1234 break;
1235
1236 case NestedNameSpecifier::TypeSpec:
1237 case NestedNameSpecifier::TypeSpecWithTemplate:
1238 if (Visit(Q.getTypeLoc()))
1239 return true;
1240
1241 break;
1242
1243 case NestedNameSpecifier::Global:
1244 case NestedNameSpecifier::Identifier:
1245 break;
1246 }
1247 }
1248
1249 return false;
1250}
1251
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001252bool CursorVisitor::VisitTemplateParameters(
1253 const TemplateParameterList *Params) {
1254 if (!Params)
1255 return false;
1256
1257 for (TemplateParameterList::const_iterator P = Params->begin(),
1258 PEnd = Params->end();
1259 P != PEnd; ++P) {
1260 if (Visit(MakeCXCursor(*P, TU)))
1261 return true;
1262 }
1263
1264 return false;
1265}
1266
Douglas Gregor0b36e612010-08-31 20:37:03 +00001267bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1268 switch (Name.getKind()) {
1269 case TemplateName::Template:
1270 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1271
1272 case TemplateName::OverloadedTemplate:
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001273 // Visit the overloaded template set.
1274 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1275 return true;
1276
Douglas Gregor0b36e612010-08-31 20:37:03 +00001277 return false;
1278
1279 case TemplateName::DependentTemplate:
1280 // FIXME: Visit nested-name-specifier.
1281 return false;
1282
1283 case TemplateName::QualifiedTemplate:
1284 // FIXME: Visit nested-name-specifier.
1285 return Visit(MakeCursorTemplateRef(
1286 Name.getAsQualifiedTemplateName()->getDecl(),
1287 Loc, TU));
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001288
1289 case TemplateName::SubstTemplateTemplateParmPack:
1290 return Visit(MakeCursorTemplateRef(
1291 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
1292 Loc, TU));
Douglas Gregor0b36e612010-08-31 20:37:03 +00001293 }
1294
1295 return false;
1296}
1297
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001298bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1299 switch (TAL.getArgument().getKind()) {
1300 case TemplateArgument::Null:
1301 case TemplateArgument::Integral:
Douglas Gregor87dd6972010-12-20 16:52:59 +00001302 case TemplateArgument::Pack:
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001303 return false;
1304
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001305 case TemplateArgument::Type:
1306 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1307 return Visit(TSInfo->getTypeLoc());
1308 return false;
1309
1310 case TemplateArgument::Declaration:
1311 if (Expr *E = TAL.getSourceDeclExpression())
1312 return Visit(MakeCXCursor(E, StmtParent, TU));
1313 return false;
1314
1315 case TemplateArgument::Expression:
1316 if (Expr *E = TAL.getSourceExpression())
1317 return Visit(MakeCXCursor(E, StmtParent, TU));
1318 return false;
1319
1320 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001321 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00001322 if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1323 return true;
1324
Douglas Gregora7fc9012011-01-05 18:58:31 +00001325 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
Douglas Gregor0b36e612010-08-31 20:37:03 +00001326 TAL.getTemplateNameLoc());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001327 }
1328
1329 return false;
1330}
1331
Ted Kremeneka0536d82010-05-07 01:04:29 +00001332bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1333 return VisitDeclContext(D);
1334}
1335
Douglas Gregor01829d32010-08-31 14:41:23 +00001336bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1337 return Visit(TL.getUnqualifiedLoc());
1338}
1339
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001340bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00001341 ASTContext &Context = AU->getASTContext();
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001342
1343 // Some builtin types (such as Objective-C's "id", "sel", and
1344 // "Class") have associated declarations. Create cursors for those.
1345 QualType VisitType;
1346 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001347 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001348 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001349 case BuiltinType::Char_U:
1350 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001351 case BuiltinType::Char16:
1352 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001353 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001354 case BuiltinType::UInt:
1355 case BuiltinType::ULong:
1356 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001357 case BuiltinType::UInt128:
1358 case BuiltinType::Char_S:
1359 case BuiltinType::SChar:
Chris Lattner3f59c972010-12-25 23:25:43 +00001360 case BuiltinType::WChar_U:
1361 case BuiltinType::WChar_S:
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001362 case BuiltinType::Short:
1363 case BuiltinType::Int:
1364 case BuiltinType::Long:
1365 case BuiltinType::LongLong:
1366 case BuiltinType::Int128:
1367 case BuiltinType::Float:
1368 case BuiltinType::Double:
1369 case BuiltinType::LongDouble:
1370 case BuiltinType::NullPtr:
1371 case BuiltinType::Overload:
1372 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001373 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001374
Ted Kremenekc4174cc2010-02-18 18:52:18 +00001375 case BuiltinType::ObjCId:
1376 VisitType = Context.getObjCIdType();
1377 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +00001378
1379 case BuiltinType::ObjCClass:
1380 VisitType = Context.getObjCClassType();
1381 break;
1382
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001383 case BuiltinType::ObjCSel:
1384 VisitType = Context.getObjCSelType();
1385 break;
1386 }
1387
1388 if (!VisitType.isNull()) {
1389 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001390 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001391 TU));
1392 }
1393
1394 return false;
1395}
1396
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001397bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1398 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
1399}
1400
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001401bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1402 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1403}
1404
1405bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1406 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1407}
1408
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001409bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00001410 // FIXME: We can't visit the template type parameter, because there's
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001411 // no context information with which we can match up the depth/index in the
1412 // type to the appropriate
1413 return false;
1414}
1415
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001416bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1417 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1418 return true;
1419
John McCallc12c5bb2010-05-15 11:32:37 +00001420 return false;
1421}
1422
1423bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1424 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1425 return true;
1426
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001427 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1428 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1429 TU)))
1430 return true;
1431 }
1432
1433 return false;
1434}
1435
1436bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCallc12c5bb2010-05-15 11:32:37 +00001437 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001438}
1439
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001440bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1441 return Visit(TL.getInnerLoc());
1442}
1443
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001444bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1445 return Visit(TL.getPointeeLoc());
1446}
1447
1448bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1449 return Visit(TL.getPointeeLoc());
1450}
1451
1452bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1453 return Visit(TL.getPointeeLoc());
1454}
1455
1456bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001457 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001458}
1459
1460bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001461 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001462}
1463
Douglas Gregor01829d32010-08-31 14:41:23 +00001464bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1465 bool SkipResultType) {
1466 if (!SkipResultType && Visit(TL.getResultLoc()))
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001467 return true;
1468
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001469 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
Ted Kremenek5dbacb42010-04-07 00:27:13 +00001470 if (Decl *D = TL.getArg(I))
1471 if (Visit(MakeCXCursor(D, TU)))
1472 return true;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +00001473
1474 return false;
1475}
1476
1477bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1478 if (Visit(TL.getElementLoc()))
1479 return true;
1480
1481 if (Expr *Size = TL.getSizeExpr())
1482 return Visit(MakeCXCursor(Size, StmtParent, TU));
1483
1484 return false;
1485}
1486
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001487bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1488 TemplateSpecializationTypeLoc TL) {
Douglas Gregor0b36e612010-08-31 20:37:03 +00001489 // Visit the template name.
1490 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1491 TL.getTemplateNameLoc()))
1492 return true;
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00001493
1494 // Visit the template arguments.
1495 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1496 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1497 return true;
1498
1499 return false;
1500}
1501
Douglas Gregor2332c112010-01-21 20:48:56 +00001502bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1503 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1504}
1505
1506bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1507 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1508 return Visit(TSInfo->getTypeLoc());
1509
1510 return false;
1511}
1512
Douglas Gregor2494dd02011-03-01 01:34:45 +00001513bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1514 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1515 return true;
1516
1517 return false;
1518}
1519
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001520bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1521 DependentTemplateSpecializationTypeLoc TL) {
1522 // Visit the nested-name-specifier, if there is one.
1523 if (TL.getQualifierLoc() &&
1524 VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1525 return true;
1526
1527 // Visit the template arguments.
1528 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1529 if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1530 return true;
1531
1532 return false;
1533}
1534
Douglas Gregor9e876872011-03-01 18:12:44 +00001535bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1536 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1537 return true;
1538
1539 return Visit(TL.getNamedTypeLoc());
1540}
1541
Douglas Gregor7536dd52010-12-20 02:24:11 +00001542bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1543 return Visit(TL.getPatternLoc());
1544}
1545
Ted Kremenek3064ef92010-08-27 21:34:58 +00001546bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001547 // Visit the nested-name-specifier, if present.
1548 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1549 if (VisitNestedNameSpecifierLoc(QualifierLoc))
1550 return true;
1551
Ted Kremenek3064ef92010-08-27 21:34:58 +00001552 if (D->isDefinition()) {
1553 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1554 E = D->bases_end(); I != E; ++I) {
1555 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
1556 return true;
1557 }
1558 }
1559
1560 return VisitTagDecl(D);
1561}
1562
Ted Kremenek09dfa372010-02-18 05:46:33 +00001563bool CursorVisitor::VisitAttributes(Decl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00001564 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
1565 i != e; ++i)
1566 if (Visit(MakeCXCursor(*i, D, TU)))
Ted Kremenek09dfa372010-02-18 05:46:33 +00001567 return true;
1568
1569 return false;
1570}
1571
Ted Kremenekc0e1d922010-11-11 08:05:18 +00001572//===----------------------------------------------------------------------===//
1573// Data-recursive visitor methods.
1574//===----------------------------------------------------------------------===//
1575
Ted Kremenek28a71942010-11-13 00:36:47 +00001576namespace {
Ted Kremenek035dc412010-11-13 00:36:50 +00001577#define DEF_JOB(NAME, DATA, KIND)\
1578class NAME : public VisitorJob {\
1579public:\
1580 NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
1581 static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
Ted Kremenekf64d8032010-11-18 00:02:32 +00001582 DATA *get() const { return static_cast<DATA*>(data[0]); }\
Ted Kremenek035dc412010-11-13 00:36:50 +00001583};
1584
1585DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1586DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001587DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
Ted Kremenek035dc412010-11-13 00:36:50 +00001588DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
Ted Kremenek60608ec2010-11-17 00:50:47 +00001589DEF_JOB(ExplicitTemplateArgsVisit, ExplicitTemplateArgumentList,
1590 ExplicitTemplateArgsVisitKind)
Douglas Gregor94d96292011-01-19 20:34:17 +00001591DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
Ted Kremenek035dc412010-11-13 00:36:50 +00001592#undef DEF_JOB
1593
1594class DeclVisit : public VisitorJob {
1595public:
1596 DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
1597 VisitorJob(parent, VisitorJob::DeclVisitKind,
1598 d, isFirst ? (void*) 1 : (void*) 0) {}
1599 static bool classof(const VisitorJob *VJ) {
Ted Kremenek82f3c502010-11-15 22:23:26 +00001600 return VJ->getKind() == DeclVisitKind;
Ted Kremenek035dc412010-11-13 00:36:50 +00001601 }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001602 Decl *get() const { return static_cast<Decl*>(data[0]); }
1603 bool isFirst() const { return data[1] ? true : false; }
Ted Kremenek035dc412010-11-13 00:36:50 +00001604};
Ted Kremenek035dc412010-11-13 00:36:50 +00001605class TypeLocVisit : public VisitorJob {
1606public:
1607 TypeLocVisit(TypeLoc tl, CXCursor parent) :
1608 VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1609 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1610
1611 static bool classof(const VisitorJob *VJ) {
1612 return VJ->getKind() == TypeLocVisitKind;
1613 }
1614
Ted Kremenek82f3c502010-11-15 22:23:26 +00001615 TypeLoc get() const {
Ted Kremenekf64d8032010-11-18 00:02:32 +00001616 QualType T = QualType::getFromOpaquePtr(data[0]);
1617 return TypeLoc(T, data[1]);
Ted Kremenek035dc412010-11-13 00:36:50 +00001618 }
1619};
1620
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001621class LabelRefVisit : public VisitorJob {
1622public:
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001623 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
1624 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001625 labelLoc.getPtrEncoding()) {}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001626
1627 static bool classof(const VisitorJob *VJ) {
1628 return VJ->getKind() == VisitorJob::LabelRefVisitKind;
1629 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001630 LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001631 SourceLocation getLoc() const {
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001632 return SourceLocation::getFromPtrEncoding(data[1]); }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001633};
1634class NestedNameSpecifierVisit : public VisitorJob {
1635public:
1636 NestedNameSpecifierVisit(NestedNameSpecifier *NS, SourceRange R,
1637 CXCursor parent)
1638 : VisitorJob(parent, VisitorJob::NestedNameSpecifierVisitKind,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001639 NS, R.getBegin().getPtrEncoding(),
1640 R.getEnd().getPtrEncoding()) {}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001641 static bool classof(const VisitorJob *VJ) {
1642 return VJ->getKind() == VisitorJob::NestedNameSpecifierVisitKind;
1643 }
1644 NestedNameSpecifier *get() const {
1645 return static_cast<NestedNameSpecifier*>(data[0]);
1646 }
1647 SourceRange getSourceRange() const {
1648 SourceLocation A =
1649 SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1650 SourceLocation B =
1651 SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[2]);
1652 return SourceRange(A, B);
1653 }
1654};
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001655
1656class NestedNameSpecifierLocVisit : public VisitorJob {
1657public:
1658 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
1659 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
1660 Qualifier.getNestedNameSpecifier(),
1661 Qualifier.getOpaqueData()) { }
1662
1663 static bool classof(const VisitorJob *VJ) {
1664 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
1665 }
1666
1667 NestedNameSpecifierLoc get() const {
1668 return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]),
1669 data[1]);
1670 }
1671};
1672
Ted Kremenekf64d8032010-11-18 00:02:32 +00001673class DeclarationNameInfoVisit : public VisitorJob {
1674public:
1675 DeclarationNameInfoVisit(Stmt *S, CXCursor parent)
1676 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
1677 static bool classof(const VisitorJob *VJ) {
1678 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
1679 }
1680 DeclarationNameInfo get() const {
1681 Stmt *S = static_cast<Stmt*>(data[0]);
1682 switch (S->getStmtClass()) {
1683 default:
1684 llvm_unreachable("Unhandled Stmt");
1685 case Stmt::CXXDependentScopeMemberExprClass:
1686 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
1687 case Stmt::DependentScopeDeclRefExprClass:
1688 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
1689 }
1690 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001691};
Ted Kremenekcdba6592010-11-18 00:42:18 +00001692class MemberRefVisit : public VisitorJob {
1693public:
1694 MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent)
1695 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001696 L.getPtrEncoding()) {}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001697 static bool classof(const VisitorJob *VJ) {
1698 return VJ->getKind() == VisitorJob::MemberRefVisitKind;
1699 }
1700 FieldDecl *get() const {
1701 return static_cast<FieldDecl*>(data[0]);
1702 }
1703 SourceLocation getLoc() const {
1704 return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1705 }
1706};
Ted Kremenek28a71942010-11-13 00:36:47 +00001707class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
1708 VisitorWorkList &WL;
1709 CXCursor Parent;
1710public:
1711 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
1712 : WL(wl), Parent(parent) {}
1713
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001714 void VisitAddrLabelExpr(AddrLabelExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001715 void VisitBlockExpr(BlockExpr *B);
Ted Kremenek28a71942010-11-13 00:36:47 +00001716 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Ted Kremenek083c7e22010-11-13 05:38:03 +00001717 void VisitCompoundStmt(CompoundStmt *S);
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001718 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ }
Ted Kremenekf64d8032010-11-18 00:02:32 +00001719 void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001720 void VisitCXXNewExpr(CXXNewExpr *E);
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001721 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001722 void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001723 void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001724 void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
Ted Kremenekb8dd1ca2010-11-17 00:50:41 +00001725 void VisitCXXTypeidExpr(CXXTypeidExpr *E);
Ted Kremenek55b933a2010-11-17 00:50:36 +00001726 void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
Ted Kremenek1e7e8772010-11-17 00:50:52 +00001727 void VisitCXXUuidofExpr(CXXUuidofExpr *E);
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001728 void VisitDeclRefExpr(DeclRefExpr *D);
Ted Kremenek035dc412010-11-13 00:36:50 +00001729 void VisitDeclStmt(DeclStmt *S);
Ted Kremenekf64d8032010-11-18 00:02:32 +00001730 void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001731 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001732 void VisitExplicitCastExpr(ExplicitCastExpr *E);
1733 void VisitForStmt(ForStmt *FS);
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001734 void VisitGotoStmt(GotoStmt *GS);
Ted Kremenek28a71942010-11-13 00:36:47 +00001735 void VisitIfStmt(IfStmt *If);
1736 void VisitInitListExpr(InitListExpr *IE);
1737 void VisitMemberExpr(MemberExpr *M);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001738 void VisitOffsetOfExpr(OffsetOfExpr *E);
Ted Kremenek73d15c42010-11-13 01:09:29 +00001739 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001740 void VisitObjCMessageExpr(ObjCMessageExpr *M);
1741 void VisitOverloadExpr(OverloadExpr *E);
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001742 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001743 void VisitStmt(Stmt *S);
1744 void VisitSwitchStmt(SwitchStmt *S);
1745 void VisitWhileStmt(WhileStmt *W);
Ted Kremenek2939b6f2010-11-17 00:50:50 +00001746 void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
Francois Pichet6ad6f282010-12-07 00:08:36 +00001747 void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E);
Ted Kremenek28a71942010-11-13 00:36:47 +00001748 void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U);
Ted Kremenek9d3bf792010-11-17 00:50:43 +00001749 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor94d96292011-01-19 20:34:17 +00001750 void VisitSizeOfPackExpr(SizeOfPackExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001751
Ted Kremenek28a71942010-11-13 00:36:47 +00001752private:
Ted Kremenekf64d8032010-11-18 00:02:32 +00001753 void AddDeclarationNameInfo(Stmt *S);
1754 void AddNestedNameSpecifier(NestedNameSpecifier *NS, SourceRange R);
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001755 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
Ted Kremenek60608ec2010-11-17 00:50:47 +00001756 void AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001757 void AddMemberRef(FieldDecl *D, SourceLocation L);
Ted Kremenek28a71942010-11-13 00:36:47 +00001758 void AddStmt(Stmt *S);
Ted Kremenek035dc412010-11-13 00:36:50 +00001759 void AddDecl(Decl *D, bool isFirst = true);
Ted Kremenek28a71942010-11-13 00:36:47 +00001760 void AddTypeLoc(TypeSourceInfo *TI);
1761 void EnqueueChildren(Stmt *S);
1762};
1763} // end anonyous namespace
1764
Ted Kremenekf64d8032010-11-18 00:02:32 +00001765void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) {
1766 // 'S' should always be non-null, since it comes from the
1767 // statement we are visiting.
1768 WL.push_back(DeclarationNameInfoVisit(S, Parent));
1769}
1770void EnqueueVisitor::AddNestedNameSpecifier(NestedNameSpecifier *N,
1771 SourceRange R) {
1772 if (N)
1773 WL.push_back(NestedNameSpecifierVisit(N, R, Parent));
1774}
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001775
1776void
1777EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1778 if (Qualifier)
1779 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
1780}
1781
Ted Kremenek28a71942010-11-13 00:36:47 +00001782void EnqueueVisitor::AddStmt(Stmt *S) {
1783 if (S)
1784 WL.push_back(StmtVisit(S, Parent));
1785}
Ted Kremenek035dc412010-11-13 00:36:50 +00001786void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
Ted Kremenek28a71942010-11-13 00:36:47 +00001787 if (D)
Ted Kremenek035dc412010-11-13 00:36:50 +00001788 WL.push_back(DeclVisit(D, Parent, isFirst));
Ted Kremenek28a71942010-11-13 00:36:47 +00001789}
Ted Kremenek60608ec2010-11-17 00:50:47 +00001790void EnqueueVisitor::
1791 AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A) {
1792 if (A)
1793 WL.push_back(ExplicitTemplateArgsVisit(
1794 const_cast<ExplicitTemplateArgumentList*>(A), Parent));
1795}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001796void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) {
1797 if (D)
1798 WL.push_back(MemberRefVisit(D, L, Parent));
1799}
Ted Kremenek28a71942010-11-13 00:36:47 +00001800void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
1801 if (TI)
1802 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
1803 }
1804void EnqueueVisitor::EnqueueChildren(Stmt *S) {
Ted Kremeneka6b70432010-11-12 21:34:09 +00001805 unsigned size = WL.size();
John McCall7502c1d2011-02-13 04:07:26 +00001806 for (Stmt::child_range Child = S->children(); Child; ++Child) {
Ted Kremenek28a71942010-11-13 00:36:47 +00001807 AddStmt(*Child);
Ted Kremeneka6b70432010-11-12 21:34:09 +00001808 }
1809 if (size == WL.size())
1810 return;
1811 // Now reverse the entries we just added. This will match the DFS
1812 // ordering performed by the worklist.
1813 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1814 std::reverse(I, E);
1815}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001816void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
1817 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
1818}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001819void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) {
1820 AddDecl(B->getBlockDecl());
1821}
Ted Kremenek28a71942010-11-13 00:36:47 +00001822void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1823 EnqueueChildren(E);
1824 AddTypeLoc(E->getTypeSourceInfo());
1825}
Ted Kremenek083c7e22010-11-13 05:38:03 +00001826void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) {
1827 for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(),
1828 E = S->body_rend(); I != E; ++I) {
1829 AddStmt(*I);
1830 }
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001831}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001832void EnqueueVisitor::
1833VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
1834 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1835 AddDeclarationNameInfo(E);
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001836 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1837 AddNestedNameSpecifierLoc(QualifierLoc);
Ted Kremenekf64d8032010-11-18 00:02:32 +00001838 if (!E->isImplicitAccess())
1839 AddStmt(E->getBase());
1840}
Ted Kremenek11b8e3e2010-11-13 05:55:53 +00001841void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
1842 // Enqueue the initializer or constructor arguments.
1843 for (unsigned I = E->getNumConstructorArgs(); I > 0; --I)
1844 AddStmt(E->getConstructorArg(I-1));
1845 // Enqueue the array size, if any.
1846 AddStmt(E->getArraySize());
1847 // Enqueue the allocated type.
1848 AddTypeLoc(E->getAllocatedTypeSourceInfo());
1849 // Enqueue the placement arguments.
1850 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
1851 AddStmt(E->getPlacementArg(I-1));
1852}
Ted Kremenek28a71942010-11-13 00:36:47 +00001853void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) {
Ted Kremenek8b8d8c92010-11-13 05:55:56 +00001854 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
1855 AddStmt(CE->getArg(I-1));
Ted Kremenek28a71942010-11-13 00:36:47 +00001856 AddStmt(CE->getCallee());
1857 AddStmt(CE->getArg(0));
1858}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001859void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1860 // Visit the name of the type being destroyed.
1861 AddTypeLoc(E->getDestroyedTypeInfo());
1862 // Visit the scope type that looks disturbingly like the nested-name-specifier
1863 // but isn't.
1864 AddTypeLoc(E->getScopeTypeInfo());
1865 // Visit the nested-name-specifier.
Douglas Gregorf3db29f2011-02-25 18:19:59 +00001866 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1867 AddNestedNameSpecifierLoc(QualifierLoc);
Ted Kremenekcdba6592010-11-18 00:42:18 +00001868 // Visit base expression.
1869 AddStmt(E->getBase());
1870}
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00001871void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1872 AddTypeLoc(E->getTypeSourceInfo());
1873}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001874void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1875 EnqueueChildren(E);
1876 AddTypeLoc(E->getTypeSourceInfo());
1877}
Ted Kremenekb8dd1ca2010-11-17 00:50:41 +00001878void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1879 EnqueueChildren(E);
1880 if (E->isTypeOperand())
1881 AddTypeLoc(E->getTypeOperandSourceInfo());
1882}
Ted Kremenek55b933a2010-11-17 00:50:36 +00001883
1884void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr
1885 *E) {
1886 EnqueueChildren(E);
1887 AddTypeLoc(E->getTypeSourceInfo());
1888}
Ted Kremenek1e7e8772010-11-17 00:50:52 +00001889void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1890 EnqueueChildren(E);
1891 if (E->isTypeOperand())
1892 AddTypeLoc(E->getTypeOperandSourceInfo());
1893}
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001894void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek60608ec2010-11-17 00:50:47 +00001895 if (DR->hasExplicitTemplateArgs()) {
1896 AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
1897 }
Ted Kremeneke4979cc2010-11-13 00:58:18 +00001898 WL.push_back(DeclRefExprParts(DR, Parent));
1899}
Ted Kremenekf64d8032010-11-18 00:02:32 +00001900void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1901 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1902 AddDeclarationNameInfo(E);
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00001903 AddNestedNameSpecifierLoc(E->getQualifierLoc());
Ted Kremenekf64d8032010-11-18 00:02:32 +00001904}
Ted Kremenek035dc412010-11-13 00:36:50 +00001905void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
1906 unsigned size = WL.size();
1907 bool isFirst = true;
1908 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
1909 D != DEnd; ++D) {
1910 AddDecl(*D, isFirst);
1911 isFirst = false;
1912 }
1913 if (size == WL.size())
1914 return;
1915 // Now reverse the entries we just added. This will match the DFS
1916 // ordering performed by the worklist.
1917 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1918 std::reverse(I, E);
1919}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001920void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1921 AddStmt(E->getInit());
1922 typedef DesignatedInitExpr::Designator Designator;
1923 for (DesignatedInitExpr::reverse_designators_iterator
1924 D = E->designators_rbegin(), DEnd = E->designators_rend();
1925 D != DEnd; ++D) {
1926 if (D->isFieldDesignator()) {
1927 if (FieldDecl *Field = D->getField())
1928 AddMemberRef(Field, D->getFieldLoc());
1929 continue;
1930 }
1931 if (D->isArrayDesignator()) {
1932 AddStmt(E->getArrayIndex(*D));
1933 continue;
1934 }
1935 assert(D->isArrayRangeDesignator() && "Unknown designator kind");
1936 AddStmt(E->getArrayRangeEnd(*D));
1937 AddStmt(E->getArrayRangeStart(*D));
1938 }
1939}
Ted Kremenek28a71942010-11-13 00:36:47 +00001940void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1941 EnqueueChildren(E);
1942 AddTypeLoc(E->getTypeInfoAsWritten());
1943}
1944void EnqueueVisitor::VisitForStmt(ForStmt *FS) {
1945 AddStmt(FS->getBody());
1946 AddStmt(FS->getInc());
1947 AddStmt(FS->getCond());
1948 AddDecl(FS->getConditionVariable());
1949 AddStmt(FS->getInit());
1950}
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00001951void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) {
1952 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
1953}
Ted Kremenek28a71942010-11-13 00:36:47 +00001954void EnqueueVisitor::VisitIfStmt(IfStmt *If) {
1955 AddStmt(If->getElse());
1956 AddStmt(If->getThen());
1957 AddStmt(If->getCond());
1958 AddDecl(If->getConditionVariable());
1959}
1960void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) {
1961 // We care about the syntactic form of the initializer list, only.
1962 if (InitListExpr *Syntactic = IE->getSyntacticForm())
1963 IE = Syntactic;
1964 EnqueueChildren(IE);
1965}
1966void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) {
Douglas Gregor89629a72010-11-17 17:15:08 +00001967 WL.push_back(MemberExprParts(M, Parent));
1968
1969 // If the base of the member access expression is an implicit 'this', don't
1970 // visit it.
1971 // FIXME: If we ever want to show these implicit accesses, this will be
1972 // unfortunate. However, clang_getCursor() relies on this behavior.
1973 if (CXXThisExpr *This
1974 = llvm::dyn_cast<CXXThisExpr>(M->getBase()->IgnoreParenImpCasts()))
1975 if (This->isImplicit())
1976 return;
1977
Ted Kremenek28a71942010-11-13 00:36:47 +00001978 AddStmt(M->getBase());
1979}
Ted Kremenek73d15c42010-11-13 01:09:29 +00001980void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1981 AddTypeLoc(E->getEncodedTypeSourceInfo());
1982}
Ted Kremenek28a71942010-11-13 00:36:47 +00001983void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) {
1984 EnqueueChildren(M);
1985 AddTypeLoc(M->getClassReceiverTypeInfo());
1986}
Ted Kremenekcdba6592010-11-18 00:42:18 +00001987void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
1988 // Visit the components of the offsetof expression.
1989 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
1990 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
1991 const OffsetOfNode &Node = E->getComponent(I-1);
1992 switch (Node.getKind()) {
1993 case OffsetOfNode::Array:
1994 AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
1995 break;
1996 case OffsetOfNode::Field:
1997 AddMemberRef(Node.getField(), Node.getRange().getEnd());
1998 break;
1999 case OffsetOfNode::Identifier:
2000 case OffsetOfNode::Base:
2001 continue;
2002 }
2003 }
2004 // Visit the type into which we're computing the offset.
2005 AddTypeLoc(E->getTypeSourceInfo());
2006}
Ted Kremenek28a71942010-11-13 00:36:47 +00002007void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) {
Ted Kremenek60608ec2010-11-17 00:50:47 +00002008 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
Ted Kremenek60458782010-11-12 21:34:16 +00002009 WL.push_back(OverloadExprParts(E, Parent));
2010}
Ted Kremenek6d0a00d2010-11-17 02:18:35 +00002011void EnqueueVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2012 EnqueueChildren(E);
2013 if (E->isArgumentType())
2014 AddTypeLoc(E->getArgumentTypeInfo());
2015}
Ted Kremenek28a71942010-11-13 00:36:47 +00002016void EnqueueVisitor::VisitStmt(Stmt *S) {
2017 EnqueueChildren(S);
2018}
2019void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) {
2020 AddStmt(S->getBody());
2021 AddStmt(S->getCond());
2022 AddDecl(S->getConditionVariable());
2023}
Ted Kremenekfafa75a2010-11-17 00:50:39 +00002024
Ted Kremenek28a71942010-11-13 00:36:47 +00002025void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) {
2026 AddStmt(W->getBody());
2027 AddStmt(W->getCond());
2028 AddDecl(W->getConditionVariable());
2029}
Ted Kremenek2939b6f2010-11-17 00:50:50 +00002030void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
2031 AddTypeLoc(E->getQueriedTypeSourceInfo());
2032}
Francois Pichet6ad6f282010-12-07 00:08:36 +00002033
2034void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
Francois Pichet6ad6f282010-12-07 00:08:36 +00002035 AddTypeLoc(E->getRhsTypeSourceInfo());
Francois Pichet0a03a3f2010-12-08 09:11:05 +00002036 AddTypeLoc(E->getLhsTypeSourceInfo());
Francois Pichet6ad6f282010-12-07 00:08:36 +00002037}
2038
Ted Kremenek28a71942010-11-13 00:36:47 +00002039void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) {
2040 VisitOverloadExpr(U);
2041 if (!U->isImplicitAccess())
2042 AddStmt(U->getBase());
2043}
Ted Kremenek9d3bf792010-11-17 00:50:43 +00002044void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) {
2045 AddStmt(E->getSubExpr());
2046 AddTypeLoc(E->getWrittenTypeInfo());
2047}
Douglas Gregor94d96292011-01-19 20:34:17 +00002048void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2049 WL.push_back(SizeOfPackExprParts(E, Parent));
2050}
Ted Kremenek60458782010-11-12 21:34:16 +00002051
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002052void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) {
Ted Kremenek28a71942010-11-13 00:36:47 +00002053 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S);
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002054}
2055
2056bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
2057 if (RegionOfInterest.isValid()) {
2058 SourceRange Range = getRawCursorExtent(C);
2059 if (Range.isInvalid() || CompareRegionOfInterest(Range))
2060 return false;
2061 }
2062 return true;
2063}
2064
2065bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
2066 while (!WL.empty()) {
2067 // Dequeue the worklist item.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002068 VisitorJob LI = WL.back();
2069 WL.pop_back();
2070
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002071 // Set the Parent field, then back to its old value once we're done.
2072 SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
2073
2074 switch (LI.getKind()) {
Ted Kremenekf1107452010-11-12 18:26:56 +00002075 case VisitorJob::DeclVisitKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002076 Decl *D = cast<DeclVisit>(&LI)->get();
Ted Kremenekf1107452010-11-12 18:26:56 +00002077 if (!D)
2078 continue;
2079
2080 // For now, perform default visitation for Decls.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002081 if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst())))
Ted Kremenekf1107452010-11-12 18:26:56 +00002082 return true;
2083
2084 continue;
2085 }
Ted Kremenek60608ec2010-11-17 00:50:47 +00002086 case VisitorJob::ExplicitTemplateArgsVisitKind: {
2087 const ExplicitTemplateArgumentList *ArgList =
2088 cast<ExplicitTemplateArgsVisit>(&LI)->get();
2089 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
2090 *ArgEnd = Arg + ArgList->NumTemplateArgs;
2091 Arg != ArgEnd; ++Arg) {
2092 if (VisitTemplateArgumentLoc(*Arg))
2093 return true;
2094 }
2095 continue;
2096 }
Ted Kremenekcdb4caf2010-11-12 21:34:12 +00002097 case VisitorJob::TypeLocVisitKind: {
2098 // Perform default visitation for TypeLocs.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002099 if (Visit(cast<TypeLocVisit>(&LI)->get()))
Ted Kremenekcdb4caf2010-11-12 21:34:12 +00002100 return true;
2101 continue;
2102 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00002103 case VisitorJob::LabelRefVisitKind: {
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002104 LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
Ted Kremeneke7455012011-02-23 04:54:51 +00002105 if (LabelStmt *stmt = LS->getStmt()) {
2106 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
2107 TU))) {
2108 return true;
2109 }
2110 }
Ted Kremenekae1fd6f2010-11-17 00:50:45 +00002111 continue;
2112 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00002113
Ted Kremenekf64d8032010-11-18 00:02:32 +00002114 case VisitorJob::NestedNameSpecifierVisitKind: {
2115 NestedNameSpecifierVisit *V = cast<NestedNameSpecifierVisit>(&LI);
2116 if (VisitNestedNameSpecifier(V->get(), V->getSourceRange()))
2117 return true;
2118 continue;
2119 }
Douglas Gregorf3db29f2011-02-25 18:19:59 +00002120
2121 case VisitorJob::NestedNameSpecifierLocVisitKind: {
2122 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
2123 if (VisitNestedNameSpecifierLoc(V->get()))
2124 return true;
2125 continue;
2126 }
2127
Ted Kremenekf64d8032010-11-18 00:02:32 +00002128 case VisitorJob::DeclarationNameInfoVisitKind: {
2129 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
2130 ->get()))
2131 return true;
2132 continue;
2133 }
Ted Kremenekcdba6592010-11-18 00:42:18 +00002134 case VisitorJob::MemberRefVisitKind: {
2135 MemberRefVisit *V = cast<MemberRefVisit>(&LI);
2136 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
2137 return true;
2138 continue;
2139 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002140 case VisitorJob::StmtVisitKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002141 Stmt *S = cast<StmtVisit>(&LI)->get();
Ted Kremenek8c269ac2010-11-11 23:11:43 +00002142 if (!S)
2143 continue;
2144
Ted Kremenekf1107452010-11-12 18:26:56 +00002145 // Update the current cursor.
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002146 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU);
Ted Kremenekcdba6592010-11-18 00:42:18 +00002147 if (!IsInRegionOfInterest(Cursor))
2148 continue;
2149 switch (Visitor(Cursor, Parent, ClientData)) {
2150 case CXChildVisit_Break: return true;
2151 case CXChildVisit_Continue: break;
2152 case CXChildVisit_Recurse:
2153 EnqueueWorkList(WL, S);
Ted Kremenek82f3c502010-11-15 22:23:26 +00002154 break;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002155 }
Ted Kremenek82f3c502010-11-15 22:23:26 +00002156 continue;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002157 }
2158 case VisitorJob::MemberExprPartsKind: {
2159 // Handle the other pieces in the MemberExpr besides the base.
Ted Kremenek82f3c502010-11-15 22:23:26 +00002160 MemberExpr *M = cast<MemberExprParts>(&LI)->get();
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002161
2162 // Visit the nested-name-specifier
Douglas Gregor40d96a62011-02-28 21:54:11 +00002163 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
2164 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002165 return true;
2166
2167 // Visit the declaration name.
2168 if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
2169 return true;
2170
2171 // Visit the explicitly-specified template arguments, if any.
2172 if (M->hasExplicitTemplateArgs()) {
2173 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
2174 *ArgEnd = Arg + M->getNumTemplateArgs();
2175 Arg != ArgEnd; ++Arg) {
2176 if (VisitTemplateArgumentLoc(*Arg))
2177 return true;
2178 }
2179 }
2180 continue;
2181 }
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002182 case VisitorJob::DeclRefExprPartsKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002183 DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002184 // Visit nested-name-specifier, if present.
Douglas Gregor40d96a62011-02-28 21:54:11 +00002185 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
2186 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002187 return true;
2188 // Visit declaration name.
2189 if (VisitDeclarationNameInfo(DR->getNameInfo()))
2190 return true;
Ted Kremeneke4979cc2010-11-13 00:58:18 +00002191 continue;
2192 }
Ted Kremenek60458782010-11-12 21:34:16 +00002193 case VisitorJob::OverloadExprPartsKind: {
Ted Kremenek82f3c502010-11-15 22:23:26 +00002194 OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
Ted Kremenek60458782010-11-12 21:34:16 +00002195 // Visit the nested-name-specifier.
Douglas Gregor4c9be892011-02-28 20:01:57 +00002196 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
2197 if (VisitNestedNameSpecifierLoc(QualifierLoc))
Ted Kremenek60458782010-11-12 21:34:16 +00002198 return true;
2199 // Visit the declaration name.
2200 if (VisitDeclarationNameInfo(O->getNameInfo()))
2201 return true;
2202 // Visit the overloaded declaration reference.
2203 if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
2204 return true;
Ted Kremenek60458782010-11-12 21:34:16 +00002205 continue;
2206 }
Douglas Gregor94d96292011-01-19 20:34:17 +00002207 case VisitorJob::SizeOfPackExprPartsKind: {
2208 SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
2209 NamedDecl *Pack = E->getPack();
2210 if (isa<TemplateTypeParmDecl>(Pack)) {
2211 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
2212 E->getPackLoc(), TU)))
2213 return true;
2214
2215 continue;
2216 }
2217
2218 if (isa<TemplateTemplateParmDecl>(Pack)) {
2219 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
2220 E->getPackLoc(), TU)))
2221 return true;
2222
2223 continue;
2224 }
2225
2226 // Non-type template parameter packs and function parameter packs are
2227 // treated like DeclRefExpr cursors.
2228 continue;
2229 }
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002230 }
2231 }
2232 return false;
2233}
2234
Ted Kremenekcdba6592010-11-18 00:42:18 +00002235bool CursorVisitor::Visit(Stmt *S) {
Ted Kremenekd1ded662010-11-15 23:31:32 +00002236 VisitorWorkList *WL = 0;
2237 if (!WorkListFreeList.empty()) {
2238 WL = WorkListFreeList.back();
2239 WL->clear();
2240 WorkListFreeList.pop_back();
2241 }
2242 else {
2243 WL = new VisitorWorkList();
2244 WorkListCache.push_back(WL);
2245 }
2246 EnqueueWorkList(*WL, S);
2247 bool result = RunVisitorWorkList(*WL);
2248 WorkListFreeList.push_back(WL);
2249 return result;
Ted Kremenekc0e1d922010-11-11 08:05:18 +00002250}
2251
2252//===----------------------------------------------------------------------===//
2253// Misc. API hooks.
2254//===----------------------------------------------------------------------===//
2255
Douglas Gregor8c8d5412010-09-24 21:18:36 +00002256static llvm::sys::Mutex EnableMultithreadingMutex;
2257static bool EnabledMultithreading;
2258
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00002259extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002260CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
2261 int displayDiagnostics) {
Daniel Dunbar48615ff2010-10-08 19:30:33 +00002262 // Disable pretty stack trace functionality, which will otherwise be a very
2263 // poor citizen of the world and set up all sorts of signal handlers.
2264 llvm::DisablePrettyStackTrace = true;
2265
Daniel Dunbarc7df4f32010-08-18 18:43:14 +00002266 // We use crash recovery to make some of our APIs more reliable, implicitly
2267 // enable it.
2268 llvm::CrashRecoveryContext::Enable();
2269
Douglas Gregor8c8d5412010-09-24 21:18:36 +00002270 // Enable support for multithreading in LLVM.
2271 {
2272 llvm::sys::ScopedLock L(EnableMultithreadingMutex);
2273 if (!EnabledMultithreading) {
2274 llvm::llvm_start_multithreaded();
2275 EnabledMultithreading = true;
2276 }
2277 }
2278
Douglas Gregora030b7c2010-01-22 20:35:53 +00002279 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002280 if (excludeDeclarationsFromPCH)
2281 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002282 if (displayDiagnostics)
2283 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002284 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +00002285}
2286
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002287void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002288 if (CIdx)
2289 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00002290}
2291
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002292CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +00002293 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002294 if (!CIdx)
2295 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002296
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00002297 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002298 FileSystemOptions FileSystemOpts;
2299 FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002300
Douglas Gregor28019772010-04-05 23:52:57 +00002301 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002302 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
Douglas Gregora88084b2010-02-18 18:08:43 +00002303 CXXIdx->getOnlyLocalDecls(),
2304 0, 0, true);
Ted Kremeneka60ed472010-11-16 08:15:36 +00002305 return MakeCXTranslationUnit(TU);
Steve Naroff600866c2009-08-27 19:51:58 +00002306}
2307
Douglas Gregorb1c031b2010-08-09 22:28:58 +00002308unsigned clang_defaultEditingTranslationUnitOptions() {
Douglas Gregor2a2c50b2010-09-27 05:49:58 +00002309 return CXTranslationUnit_PrecompiledPreamble |
Douglas Gregor99ba2022010-10-27 17:24:53 +00002310 CXTranslationUnit_CacheCompletionResults |
2311 CXTranslationUnit_CXXPrecompiledPreamble;
Douglas Gregorb1c031b2010-08-09 22:28:58 +00002312}
2313
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002314CXTranslationUnit
2315clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
2316 const char *source_filename,
2317 int num_command_line_args,
Douglas Gregor2ef69442010-09-01 16:43:19 +00002318 const char * const *command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +00002319 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00002320 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor5a430212010-07-21 18:52:53 +00002321 return clang_parseTranslationUnit(CIdx, source_filename,
2322 command_line_args, num_command_line_args,
2323 unsaved_files, num_unsaved_files,
2324 CXTranslationUnit_DetailedPreprocessingRecord);
2325}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002326
2327struct ParseTranslationUnitInfo {
2328 CXIndex CIdx;
2329 const char *source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002330 const char *const *command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002331 int num_command_line_args;
2332 struct CXUnsavedFile *unsaved_files;
2333 unsigned num_unsaved_files;
2334 unsigned options;
2335 CXTranslationUnit result;
2336};
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002337static void clang_parseTranslationUnit_Impl(void *UserData) {
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002338 ParseTranslationUnitInfo *PTUI =
2339 static_cast<ParseTranslationUnitInfo*>(UserData);
2340 CXIndex CIdx = PTUI->CIdx;
2341 const char *source_filename = PTUI->source_filename;
Douglas Gregor2ef69442010-09-01 16:43:19 +00002342 const char * const *command_line_args = PTUI->command_line_args;
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002343 int num_command_line_args = PTUI->num_command_line_args;
2344 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
2345 unsigned num_unsaved_files = PTUI->num_unsaved_files;
2346 unsigned options = PTUI->options;
2347 PTUI->result = 0;
Douglas Gregor5a430212010-07-21 18:52:53 +00002348
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002349 if (!CIdx)
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002350 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002351
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002352 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2353
Douglas Gregor44c181a2010-07-23 00:33:23 +00002354 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
Douglas Gregordf95a132010-08-09 20:45:32 +00002355 bool CompleteTranslationUnit
2356 = ((options & CXTranslationUnit_Incomplete) == 0);
Douglas Gregor87c08a52010-08-13 22:48:40 +00002357 bool CacheCodeCompetionResults
2358 = options & CXTranslationUnit_CacheCompletionResults;
Douglas Gregor99ba2022010-10-27 17:24:53 +00002359 bool CXXPrecompilePreamble
2360 = options & CXTranslationUnit_CXXPrecompiledPreamble;
2361 bool CXXChainedPCH
2362 = options & CXTranslationUnit_CXXChainedPCH;
Douglas Gregor87c08a52010-08-13 22:48:40 +00002363
Douglas Gregor5352ac02010-01-28 00:27:43 +00002364 // Configure the diagnostics.
2365 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +00002366 llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Douglas Gregor0b53cf82011-01-19 01:02:47 +00002367 Diags = CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
2368 command_line_args);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002369
Douglas Gregor4db64a42010-01-23 00:14:00 +00002370 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2371 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattnera0a270c2010-04-05 22:42:27 +00002372 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002373 const llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00002374 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +00002375 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2376 Buffer));
2377 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002378
Douglas Gregorb10daed2010-10-11 16:52:23 +00002379 llvm::SmallVector<const char *, 16> Args;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002380
Ted Kremenek139ba862009-10-22 00:03:57 +00002381 // The 'source_filename' argument is optional. If the caller does not
2382 // specify it then it is assumed that the source file is specified
2383 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002384 if (source_filename)
Douglas Gregorb10daed2010-10-11 16:52:23 +00002385 Args.push_back(source_filename);
Douglas Gregor52ddc5d2010-07-09 18:39:07 +00002386
2387 // Since the Clang C library is primarily used by batch tools dealing with
2388 // (often very broken) source code, where spell-checking can have a
2389 // significant negative impact on performance (particularly when
2390 // precompiled headers are involved), we disable it by default.
Douglas Gregorb10daed2010-10-11 16:52:23 +00002391 // Only do this if we haven't found a spell-checking-related argument.
2392 bool FoundSpellCheckingArgument = false;
2393 for (int I = 0; I != num_command_line_args; ++I) {
2394 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
2395 strcmp(command_line_args[I], "-fspell-checking") == 0) {
2396 FoundSpellCheckingArgument = true;
2397 break;
Steve Naroffe56b4ba2009-10-20 14:46:24 +00002398 }
Douglas Gregorb10daed2010-10-11 16:52:23 +00002399 }
2400 if (!FoundSpellCheckingArgument)
2401 Args.push_back("-fno-spell-checking");
2402
2403 Args.insert(Args.end(), command_line_args,
2404 command_line_args + num_command_line_args);
Douglas Gregord93256e2010-01-28 06:00:51 +00002405
Douglas Gregor44c181a2010-07-23 00:33:23 +00002406 // Do we need the detailed preprocessing record?
2407 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
Douglas Gregorb10daed2010-10-11 16:52:23 +00002408 Args.push_back("-Xclang");
2409 Args.push_back("-detailed-preprocessing-record");
Douglas Gregor44c181a2010-07-23 00:33:23 +00002410 }
2411
Argyrios Kyrtzidis026f6912010-11-18 21:47:04 +00002412 unsigned NumErrors = Diags->getClient()->getNumErrors();
Douglas Gregorb10daed2010-10-11 16:52:23 +00002413 llvm::OwningPtr<ASTUnit> Unit(
2414 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
2415 Diags,
2416 CXXIdx->getClangResourcesPath(),
2417 CXXIdx->getOnlyLocalDecls(),
Douglas Gregore47be3e2010-11-11 00:39:14 +00002418 /*CaptureDiagnostics=*/true,
Douglas Gregorb10daed2010-10-11 16:52:23 +00002419 RemappedFiles.data(),
2420 RemappedFiles.size(),
Douglas Gregorb10daed2010-10-11 16:52:23 +00002421 PrecompilePreamble,
2422 CompleteTranslationUnit,
Douglas Gregor99ba2022010-10-27 17:24:53 +00002423 CacheCodeCompetionResults,
2424 CXXPrecompilePreamble,
2425 CXXChainedPCH));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002426
Argyrios Kyrtzidis026f6912010-11-18 21:47:04 +00002427 if (NumErrors != Diags->getClient()->getNumErrors()) {
Douglas Gregorb10daed2010-10-11 16:52:23 +00002428 // Make sure to check that 'Unit' is non-NULL.
2429 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
2430 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
2431 DEnd = Unit->stored_diag_end();
2432 D != DEnd; ++D) {
2433 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
2434 CXString Msg = clang_formatDiagnostic(&Diag,
2435 clang_defaultDiagnosticDisplayOptions());
2436 fprintf(stderr, "%s\n", clang_getCString(Msg));
2437 clang_disposeString(Msg);
2438 }
Douglas Gregor274f1902010-02-22 23:17:23 +00002439#ifdef LLVM_ON_WIN32
Douglas Gregorb10daed2010-10-11 16:52:23 +00002440 // On Windows, force a flush, since there may be multiple copies of
2441 // stderr and stdout in the file system, all with different buffers
2442 // but writing to the same device.
2443 fflush(stderr);
2444#endif
2445 }
Douglas Gregora88084b2010-02-18 18:08:43 +00002446 }
Douglas Gregord93256e2010-01-28 06:00:51 +00002447
Ted Kremeneka60ed472010-11-16 08:15:36 +00002448 PTUI->result = MakeCXTranslationUnit(Unit.take());
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002449}
2450CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
2451 const char *source_filename,
Douglas Gregor2ef69442010-09-01 16:43:19 +00002452 const char * const *command_line_args,
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002453 int num_command_line_args,
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002454 struct CXUnsavedFile *unsaved_files,
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002455 unsigned num_unsaved_files,
2456 unsigned options) {
2457 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002458 num_command_line_args, unsaved_files,
2459 num_unsaved_files, options, 0 };
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002460 llvm::CrashRecoveryContext CRC;
2461
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00002462 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
Daniel Dunbar60a45432010-08-23 22:35:34 +00002463 fprintf(stderr, "libclang: crash detected during parsing: {\n");
2464 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
2465 fprintf(stderr, " 'command_line_args' : [");
2466 for (int i = 0; i != num_command_line_args; ++i) {
2467 if (i)
2468 fprintf(stderr, ", ");
2469 fprintf(stderr, "'%s'", command_line_args[i]);
2470 }
2471 fprintf(stderr, "],\n");
2472 fprintf(stderr, " 'unsaved_files' : [");
2473 for (unsigned i = 0; i != num_unsaved_files; ++i) {
2474 if (i)
2475 fprintf(stderr, ", ");
2476 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
2477 unsaved_files[i].Length);
2478 }
2479 fprintf(stderr, "],\n");
2480 fprintf(stderr, " 'options' : %d,\n", options);
2481 fprintf(stderr, "}\n");
2482
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002483 return 0;
2484 }
2485
2486 return PTUI.result;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00002487}
2488
Douglas Gregor19998442010-08-13 15:35:05 +00002489unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
2490 return CXSaveTranslationUnit_None;
2491}
2492
2493int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
2494 unsigned options) {
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002495 if (!TU)
2496 return 1;
2497
Ted Kremeneka60ed472010-11-16 08:15:36 +00002498 return static_cast<ASTUnit *>(TU->TUData)->Save(FileName);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002499}
Daniel Dunbar19ffd492010-08-18 18:43:17 +00002500
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002501void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002502 if (CTUnit) {
2503 // If the translation unit has been marked as unsafe to free, just discard
2504 // it.
Ted Kremeneka60ed472010-11-16 08:15:36 +00002505 if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree())
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002506 return;
2507
Ted Kremeneka60ed472010-11-16 08:15:36 +00002508 delete static_cast<ASTUnit *>(CTUnit->TUData);
2509 disposeCXStringPool(CTUnit->StringPool);
2510 delete CTUnit;
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002511 }
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00002512}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00002513
Douglas Gregore1e13bf2010-08-11 15:58:42 +00002514unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
2515 return CXReparse_None;
2516}
2517
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002518struct ReparseTranslationUnitInfo {
2519 CXTranslationUnit TU;
2520 unsigned num_unsaved_files;
2521 struct CXUnsavedFile *unsaved_files;
2522 unsigned options;
2523 int result;
2524};
Douglas Gregor593b0c12010-09-23 18:47:53 +00002525
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002526static void clang_reparseTranslationUnit_Impl(void *UserData) {
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002527 ReparseTranslationUnitInfo *RTUI =
2528 static_cast<ReparseTranslationUnitInfo*>(UserData);
2529 CXTranslationUnit TU = RTUI->TU;
2530 unsigned num_unsaved_files = RTUI->num_unsaved_files;
2531 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
2532 unsigned options = RTUI->options;
2533 (void) options;
2534 RTUI->result = 1;
2535
Douglas Gregorabc563f2010-07-19 21:46:24 +00002536 if (!TU)
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002537 return;
Douglas Gregor593b0c12010-09-23 18:47:53 +00002538
Ted Kremeneka60ed472010-11-16 08:15:36 +00002539 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor593b0c12010-09-23 18:47:53 +00002540 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002541
2542 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
2543 for (unsigned I = 0; I != num_unsaved_files; ++I) {
2544 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2545 const llvm::MemoryBuffer *Buffer
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00002546 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
Douglas Gregorabc563f2010-07-19 21:46:24 +00002547 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
2548 Buffer));
2549 }
2550
Douglas Gregor593b0c12010-09-23 18:47:53 +00002551 if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size()))
2552 RTUI->result = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00002553}
Douglas Gregor593b0c12010-09-23 18:47:53 +00002554
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002555int clang_reparseTranslationUnit(CXTranslationUnit TU,
2556 unsigned num_unsaved_files,
2557 struct CXUnsavedFile *unsaved_files,
2558 unsigned options) {
2559 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
2560 options, 0 };
2561 llvm::CrashRecoveryContext CRC;
2562
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00002563 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +00002564 fprintf(stderr, "libclang: crash detected during reparsing\n");
Ted Kremeneka60ed472010-11-16 08:15:36 +00002565 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002566 return 1;
2567 }
2568
Ted Kremenek1dfb26a2010-10-29 01:06:50 +00002569
Daniel Dunbarea94bbc2010-08-18 23:09:31 +00002570 return RTUI.result;
2571}
2572
Douglas Gregordf95a132010-08-09 20:45:32 +00002573
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002574CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00002575 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002576 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002577
Ted Kremeneka60ed472010-11-16 08:15:36 +00002578 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002579 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00002580}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00002581
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002582CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00002583 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002584 return Result;
2585}
2586
Ted Kremenekfb480492010-01-13 21:46:36 +00002587} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00002588
Ted Kremenekfb480492010-01-13 21:46:36 +00002589//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00002590// CXSourceLocation and CXSourceRange Operations.
2591//===----------------------------------------------------------------------===//
2592
Douglas Gregorb9790342010-01-22 21:44:22 +00002593extern "C" {
2594CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00002595 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00002596 return Result;
2597}
2598
2599unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00002600 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
2601 loc1.ptr_data[1] == loc2.ptr_data[1] &&
2602 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00002603}
2604
2605CXSourceLocation clang_getLocation(CXTranslationUnit tu,
2606 CXFile file,
2607 unsigned line,
2608 unsigned column) {
Douglas Gregor42748ec2010-04-30 19:45:53 +00002609 if (!tu || !file)
Douglas Gregorb9790342010-01-22 21:44:22 +00002610 return clang_getNullLocation();
Douglas Gregor42748ec2010-04-30 19:45:53 +00002611
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002612 bool Logging = ::getenv("LIBCLANG_LOGGING");
Ted Kremeneka60ed472010-11-16 08:15:36 +00002613 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002614 const FileEntry *File = static_cast<const FileEntry *>(file);
Douglas Gregorb9790342010-01-22 21:44:22 +00002615 SourceLocation SLoc
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00002616 = CXXUnit->getSourceManager().getLocation(File, line, column);
2617 if (SLoc.isInvalid()) {
2618 if (Logging)
2619 llvm::errs() << "clang_getLocation(\"" << File->getName()
2620 << "\", " << line << ", " << column << ") = invalid\n";
2621 return clang_getNullLocation();
2622 }
2623
2624 if (Logging)
2625 llvm::errs() << "clang_getLocation(\"" << File->getName()
2626 << "\", " << line << ", " << column << ") = "
2627 << SLoc.getRawEncoding() << "\n";
David Chisnall83889a72010-10-15 17:07:39 +00002628
2629 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
2630}
2631
2632CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu,
2633 CXFile file,
2634 unsigned offset) {
2635 if (!tu || !file)
2636 return clang_getNullLocation();
2637
Ted Kremeneka60ed472010-11-16 08:15:36 +00002638 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
David Chisnall83889a72010-10-15 17:07:39 +00002639 SourceLocation Start
2640 = CXXUnit->getSourceManager().getLocation(
2641 static_cast<const FileEntry *>(file),
2642 1, 1);
2643 if (Start.isInvalid()) return clang_getNullLocation();
2644
2645 SourceLocation SLoc = Start.getFileLocWithOffset(offset);
2646
2647 if (SLoc.isInvalid()) return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002648
Ted Kremenek1a9a0bc2010-06-28 23:54:17 +00002649 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00002650}
2651
Douglas Gregor5352ac02010-01-28 00:27:43 +00002652CXSourceRange clang_getNullRange() {
2653 CXSourceRange Result = { { 0, 0 }, 0, 0 };
2654 return Result;
2655}
Daniel Dunbard52864b2010-02-14 10:02:57 +00002656
Douglas Gregor5352ac02010-01-28 00:27:43 +00002657CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
2658 if (begin.ptr_data[0] != end.ptr_data[0] ||
2659 begin.ptr_data[1] != end.ptr_data[1])
2660 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002661
2662 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002663 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00002664 return Result;
2665}
2666
Douglas Gregor46766dc2010-01-26 19:19:08 +00002667void clang_getInstantiationLocation(CXSourceLocation location,
2668 CXFile *file,
2669 unsigned *line,
2670 unsigned *column,
2671 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00002672 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
2673
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002674 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00002675 if (file)
2676 *file = 0;
2677 if (line)
2678 *line = 0;
2679 if (column)
2680 *column = 0;
2681 if (offset)
2682 *offset = 0;
2683 return;
2684 }
2685
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002686 const SourceManager &SM =
2687 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002688 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00002689
2690 if (file)
2691 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
2692 if (line)
2693 *line = SM.getInstantiationLineNumber(InstLoc);
2694 if (column)
2695 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00002696 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00002697 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00002698}
2699
Douglas Gregora9b06d42010-11-09 06:24:54 +00002700void clang_getSpellingLocation(CXSourceLocation location,
2701 CXFile *file,
2702 unsigned *line,
2703 unsigned *column,
2704 unsigned *offset) {
2705 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
2706
2707 if (!location.ptr_data[0] || Loc.isInvalid()) {
2708 if (file)
2709 *file = 0;
2710 if (line)
2711 *line = 0;
2712 if (column)
2713 *column = 0;
2714 if (offset)
2715 *offset = 0;
2716 return;
2717 }
2718
2719 const SourceManager &SM =
2720 *static_cast<const SourceManager*>(location.ptr_data[0]);
2721 SourceLocation SpellLoc = Loc;
2722 if (SpellLoc.isMacroID()) {
2723 SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);
2724 if (SimpleSpellingLoc.isFileID() &&
2725 SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))
2726 SpellLoc = SimpleSpellingLoc;
2727 else
2728 SpellLoc = SM.getInstantiationLoc(SpellLoc);
2729 }
2730
2731 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
2732 FileID FID = LocInfo.first;
2733 unsigned FileOffset = LocInfo.second;
2734
2735 if (file)
2736 *file = (void *)SM.getFileEntryForID(FID);
2737 if (line)
2738 *line = SM.getLineNumber(FID, FileOffset);
2739 if (column)
2740 *column = SM.getColumnNumber(FID, FileOffset);
2741 if (offset)
2742 *offset = FileOffset;
2743}
2744
Douglas Gregor1db19de2010-01-19 21:36:55 +00002745CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002746 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002747 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002748 return Result;
2749}
2750
2751CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00002752 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00002753 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00002754 return Result;
2755}
2756
Douglas Gregorb9790342010-01-22 21:44:22 +00002757} // end: extern "C"
2758
Douglas Gregor1db19de2010-01-19 21:36:55 +00002759//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002760// CXFile Operations.
2761//===----------------------------------------------------------------------===//
2762
2763extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00002764CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002765 if (!SFile)
Ted Kremeneka60ed472010-11-16 08:15:36 +00002766 return createCXString((const char*)NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002767
Steve Naroff88145032009-10-27 14:35:18 +00002768 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00002769 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00002770}
2771
2772time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00002773 if (!SFile)
2774 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002775
Steve Naroff88145032009-10-27 14:35:18 +00002776 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
2777 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00002778}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002779
Douglas Gregorb9790342010-01-22 21:44:22 +00002780CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
2781 if (!tu)
2782 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002783
Ted Kremeneka60ed472010-11-16 08:15:36 +00002784 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002785
Douglas Gregorb9790342010-01-22 21:44:22 +00002786 FileManager &FMgr = CXXUnit->getFileManager();
Chris Lattner39b49bc2010-11-23 08:35:12 +00002787 return const_cast<FileEntry *>(FMgr.getFile(file_name));
Douglas Gregorb9790342010-01-22 21:44:22 +00002788}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002789
Ted Kremenekfb480492010-01-13 21:46:36 +00002790} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00002791
Ted Kremenekfb480492010-01-13 21:46:36 +00002792//===----------------------------------------------------------------------===//
2793// CXCursor Operations.
2794//===----------------------------------------------------------------------===//
2795
Ted Kremenekfb480492010-01-13 21:46:36 +00002796static Decl *getDeclFromExpr(Stmt *E) {
Douglas Gregordb1314e2010-10-01 21:11:22 +00002797 if (CastExpr *CE = dyn_cast<CastExpr>(E))
2798 return getDeclFromExpr(CE->getSubExpr());
2799
Ted Kremenekfb480492010-01-13 21:46:36 +00002800 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
2801 return RefExpr->getDecl();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002802 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2803 return RefExpr->getDecl();
Ted Kremenekfb480492010-01-13 21:46:36 +00002804 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
2805 return ME->getMemberDecl();
2806 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
2807 return RE->getDecl();
Douglas Gregordb1314e2010-10-01 21:11:22 +00002808 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
John McCall12f78a62010-12-02 01:19:52 +00002809 return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0;
Douglas Gregordb1314e2010-10-01 21:11:22 +00002810
Ted Kremenekfb480492010-01-13 21:46:36 +00002811 if (CallExpr *CE = dyn_cast<CallExpr>(E))
2812 return getDeclFromExpr(CE->getCallee());
Douglas Gregor93798e22010-11-05 21:11:19 +00002813 if (CXXConstructExpr *CE = llvm::dyn_cast<CXXConstructExpr>(E))
2814 if (!CE->isElidable())
2815 return CE->getConstructor();
Ted Kremenekfb480492010-01-13 21:46:36 +00002816 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
2817 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002818
Douglas Gregordb1314e2010-10-01 21:11:22 +00002819 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
2820 return PE->getProtocol();
Douglas Gregorc7793c72011-01-15 01:15:58 +00002821 if (SubstNonTypeTemplateParmPackExpr *NTTP
2822 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
2823 return NTTP->getParameterPack();
Douglas Gregor94d96292011-01-19 20:34:17 +00002824 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2825 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
2826 isa<ParmVarDecl>(SizeOfPack->getPack()))
2827 return SizeOfPack->getPack();
Douglas Gregordb1314e2010-10-01 21:11:22 +00002828
Ted Kremenekfb480492010-01-13 21:46:36 +00002829 return 0;
2830}
2831
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002832static SourceLocation getLocationFromExpr(Expr *E) {
2833 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
2834 return /*FIXME:*/Msg->getLeftLoc();
2835 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
2836 return DRE->getLocation();
Douglas Gregor38f28c12010-10-22 22:24:08 +00002837 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2838 return RefExpr->getLocation();
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002839 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
2840 return Member->getMemberLoc();
2841 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
2842 return Ivar->getLocation();
Douglas Gregor94d96292011-01-19 20:34:17 +00002843 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2844 return SizeOfPack->getPackLoc();
2845
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00002846 return E->getLocStart();
2847}
2848
Ted Kremenekfb480492010-01-13 21:46:36 +00002849extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002850
2851unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00002852 CXCursorVisitor visitor,
2853 CXClientData client_data) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00002854 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
2855 getCursorASTUnit(parent)->getMaxPCHLevel());
Douglas Gregorb1373d02010-01-20 20:59:29 +00002856 return CursorVis.VisitChildren(parent);
2857}
2858
David Chisnall3387c652010-11-03 14:12:26 +00002859#ifndef __has_feature
2860#define __has_feature(x) 0
2861#endif
2862#if __has_feature(blocks)
2863typedef enum CXChildVisitResult
2864 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
2865
2866static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2867 CXClientData client_data) {
2868 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2869 return block(cursor, parent);
2870}
2871#else
2872// If we are compiled with a compiler that doesn't have native blocks support,
2873// define and call the block manually, so the
2874typedef struct _CXChildVisitResult
2875{
2876 void *isa;
2877 int flags;
2878 int reserved;
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002879 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
2880 CXCursor);
David Chisnall3387c652010-11-03 14:12:26 +00002881} *CXCursorVisitorBlock;
2882
2883static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2884 CXClientData client_data) {
2885 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2886 return block->invoke(block, cursor, parent);
2887}
2888#endif
2889
2890
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00002891unsigned clang_visitChildrenWithBlock(CXCursor parent,
2892 CXCursorVisitorBlock block) {
David Chisnall3387c652010-11-03 14:12:26 +00002893 return clang_visitChildren(parent, visitWithBlock, block);
2894}
2895
Douglas Gregor78205d42010-01-20 21:45:58 +00002896static CXString getDeclSpelling(Decl *D) {
2897 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
Douglas Gregore3c60a72010-11-17 00:13:31 +00002898 if (!ND) {
2899 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
2900 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
2901 return createCXString(Property->getIdentifier()->getName());
2902
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002903 return createCXString("");
Douglas Gregore3c60a72010-11-17 00:13:31 +00002904 }
2905
Douglas Gregor78205d42010-01-20 21:45:58 +00002906 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002907 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002908
Douglas Gregor78205d42010-01-20 21:45:58 +00002909 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
2910 // No, this isn't the same as the code below. getIdentifier() is non-virtual
2911 // and returns different names. NamedDecl returns the class name and
2912 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002913 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002914
Douglas Gregor0a35bce2010-09-01 03:07:18 +00002915 if (isa<UsingDirectiveDecl>(D))
2916 return createCXString("");
2917
Ted Kremenek50aa6ac2010-05-19 21:51:10 +00002918 llvm::SmallString<1024> S;
2919 llvm::raw_svector_ostream os(S);
2920 ND->printName(os);
2921
2922 return createCXString(os.str());
Douglas Gregor78205d42010-01-20 21:45:58 +00002923}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002924
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00002925CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002926 if (clang_isTranslationUnit(C.kind))
Ted Kremeneka60ed472010-11-16 08:15:36 +00002927 return clang_getTranslationUnitSpelling(
2928 static_cast<CXTranslationUnit>(C.data[2]));
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00002929
Steve Narofff334b4e2009-09-02 18:26:48 +00002930 if (clang_isReference(C.kind)) {
2931 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00002932 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00002933 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002934 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002935 }
2936 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00002937 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002938 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002939 }
2940 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00002941 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00002942 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002943 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00002944 }
Ted Kremenek3064ef92010-08-27 21:34:58 +00002945 case CXCursor_CXXBaseSpecifier: {
2946 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
2947 return createCXString(B->getType().getAsString());
2948 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002949 case CXCursor_TypeRef: {
2950 TypeDecl *Type = getCursorTypeRef(C).first;
2951 assert(Type && "Missing type decl");
2952
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002953 return createCXString(getCursorContext(C).getTypeDeclType(Type).
2954 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002955 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00002956 case CXCursor_TemplateRef: {
2957 TemplateDecl *Template = getCursorTemplateRef(C).first;
Douglas Gregor69319002010-08-31 23:48:11 +00002958 assert(Template && "Missing template decl");
Douglas Gregor0b36e612010-08-31 20:37:03 +00002959
2960 return createCXString(Template->getNameAsString());
2961 }
Douglas Gregor69319002010-08-31 23:48:11 +00002962
2963 case CXCursor_NamespaceRef: {
2964 NamedDecl *NS = getCursorNamespaceRef(C).first;
2965 assert(NS && "Missing namespace decl");
2966
2967 return createCXString(NS->getNameAsString());
2968 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00002969
Douglas Gregora67e03f2010-09-09 21:42:20 +00002970 case CXCursor_MemberRef: {
2971 FieldDecl *Field = getCursorMemberRef(C).first;
2972 assert(Field && "Missing member decl");
2973
2974 return createCXString(Field->getNameAsString());
2975 }
2976
Douglas Gregor36897b02010-09-10 00:22:18 +00002977 case CXCursor_LabelRef: {
2978 LabelStmt *Label = getCursorLabelRef(C).first;
2979 assert(Label && "Missing label");
2980
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002981 return createCXString(Label->getName());
Douglas Gregor36897b02010-09-10 00:22:18 +00002982 }
2983
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00002984 case CXCursor_OverloadedDeclRef: {
2985 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
2986 if (Decl *D = Storage.dyn_cast<Decl *>()) {
2987 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
2988 return createCXString(ND->getNameAsString());
2989 return createCXString("");
2990 }
2991 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
2992 return createCXString(E->getName().getAsString());
2993 OverloadedTemplateStorage *Ovl
2994 = Storage.get<OverloadedTemplateStorage*>();
2995 if (Ovl->size() == 0)
2996 return createCXString("");
2997 return createCXString((*Ovl->begin())->getNameAsString());
2998 }
2999
Daniel Dunbaracca7252009-11-30 20:42:49 +00003000 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003001 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00003002 }
3003 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003004
3005 if (clang_isExpression(C.kind)) {
3006 Decl *D = getDeclFromExpr(getCursorExpr(C));
3007 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00003008 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003009 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00003010 }
3011
Douglas Gregor36897b02010-09-10 00:22:18 +00003012 if (clang_isStatement(C.kind)) {
3013 Stmt *S = getCursorStmt(C);
3014 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003015 return createCXString(Label->getName());
Douglas Gregor36897b02010-09-10 00:22:18 +00003016
3017 return createCXString("");
3018 }
3019
Douglas Gregor4ae8f292010-03-18 17:52:52 +00003020 if (C.kind == CXCursor_MacroInstantiation)
3021 return createCXString(getCursorMacroInstantiation(C)->getName()
3022 ->getNameStart());
3023
Douglas Gregor572feb22010-03-18 18:04:21 +00003024 if (C.kind == CXCursor_MacroDefinition)
3025 return createCXString(getCursorMacroDefinition(C)->getName()
3026 ->getNameStart());
3027
Douglas Gregorecdcb882010-10-20 22:00:55 +00003028 if (C.kind == CXCursor_InclusionDirective)
3029 return createCXString(getCursorInclusionDirective(C)->getFileName());
3030
Douglas Gregor60cbfac2010-01-25 16:56:17 +00003031 if (clang_isDeclaration(C.kind))
3032 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00003033
Ted Kremenekee4db4f2010-02-17 00:41:08 +00003034 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00003035}
3036
Douglas Gregor358559d2010-10-02 22:49:11 +00003037CXString clang_getCursorDisplayName(CXCursor C) {
3038 if (!clang_isDeclaration(C.kind))
3039 return clang_getCursorSpelling(C);
3040
3041 Decl *D = getCursorDecl(C);
3042 if (!D)
3043 return createCXString("");
3044
3045 PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy;
3046 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
3047 D = FunTmpl->getTemplatedDecl();
3048
3049 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
3050 llvm::SmallString<64> Str;
3051 llvm::raw_svector_ostream OS(Str);
3052 OS << Function->getNameAsString();
3053 if (Function->getPrimaryTemplate())
3054 OS << "<>";
3055 OS << "(";
3056 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
3057 if (I)
3058 OS << ", ";
3059 OS << Function->getParamDecl(I)->getType().getAsString(Policy);
3060 }
3061
3062 if (Function->isVariadic()) {
3063 if (Function->getNumParams())
3064 OS << ", ";
3065 OS << "...";
3066 }
3067 OS << ")";
3068 return createCXString(OS.str());
3069 }
3070
3071 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
3072 llvm::SmallString<64> Str;
3073 llvm::raw_svector_ostream OS(Str);
3074 OS << ClassTemplate->getNameAsString();
3075 OS << "<";
3076 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
3077 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
3078 if (I)
3079 OS << ", ";
3080
3081 NamedDecl *Param = Params->getParam(I);
3082 if (Param->getIdentifier()) {
3083 OS << Param->getIdentifier()->getName();
3084 continue;
3085 }
3086
3087 // There is no parameter name, which makes this tricky. Try to come up
3088 // with something useful that isn't too long.
3089 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3090 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
3091 else if (NonTypeTemplateParmDecl *NTTP
3092 = dyn_cast<NonTypeTemplateParmDecl>(Param))
3093 OS << NTTP->getType().getAsString(Policy);
3094 else
3095 OS << "template<...> class";
3096 }
3097
3098 OS << ">";
3099 return createCXString(OS.str());
3100 }
3101
3102 if (ClassTemplateSpecializationDecl *ClassSpec
3103 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3104 // If the type was explicitly written, use that.
3105 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
3106 return createCXString(TSInfo->getType().getAsString(Policy));
3107
3108 llvm::SmallString<64> Str;
3109 llvm::raw_svector_ostream OS(Str);
3110 OS << ClassSpec->getNameAsString();
3111 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00003112 ClassSpec->getTemplateArgs().data(),
3113 ClassSpec->getTemplateArgs().size(),
Douglas Gregor358559d2010-10-02 22:49:11 +00003114 Policy);
3115 return createCXString(OS.str());
3116 }
3117
3118 return clang_getCursorSpelling(C);
3119}
3120
Ted Kremeneke68fff62010-02-17 00:41:32 +00003121CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00003122 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003123 case CXCursor_FunctionDecl:
3124 return createCXString("FunctionDecl");
3125 case CXCursor_TypedefDecl:
3126 return createCXString("TypedefDecl");
3127 case CXCursor_EnumDecl:
3128 return createCXString("EnumDecl");
3129 case CXCursor_EnumConstantDecl:
3130 return createCXString("EnumConstantDecl");
3131 case CXCursor_StructDecl:
3132 return createCXString("StructDecl");
3133 case CXCursor_UnionDecl:
3134 return createCXString("UnionDecl");
3135 case CXCursor_ClassDecl:
3136 return createCXString("ClassDecl");
3137 case CXCursor_FieldDecl:
3138 return createCXString("FieldDecl");
3139 case CXCursor_VarDecl:
3140 return createCXString("VarDecl");
3141 case CXCursor_ParmDecl:
3142 return createCXString("ParmDecl");
3143 case CXCursor_ObjCInterfaceDecl:
3144 return createCXString("ObjCInterfaceDecl");
3145 case CXCursor_ObjCCategoryDecl:
3146 return createCXString("ObjCCategoryDecl");
3147 case CXCursor_ObjCProtocolDecl:
3148 return createCXString("ObjCProtocolDecl");
3149 case CXCursor_ObjCPropertyDecl:
3150 return createCXString("ObjCPropertyDecl");
3151 case CXCursor_ObjCIvarDecl:
3152 return createCXString("ObjCIvarDecl");
3153 case CXCursor_ObjCInstanceMethodDecl:
3154 return createCXString("ObjCInstanceMethodDecl");
3155 case CXCursor_ObjCClassMethodDecl:
3156 return createCXString("ObjCClassMethodDecl");
3157 case CXCursor_ObjCImplementationDecl:
3158 return createCXString("ObjCImplementationDecl");
3159 case CXCursor_ObjCCategoryImplDecl:
3160 return createCXString("ObjCCategoryImplDecl");
Ted Kremenek8bd5a692010-04-13 23:39:06 +00003161 case CXCursor_CXXMethod:
3162 return createCXString("CXXMethod");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003163 case CXCursor_UnexposedDecl:
3164 return createCXString("UnexposedDecl");
3165 case CXCursor_ObjCSuperClassRef:
3166 return createCXString("ObjCSuperClassRef");
3167 case CXCursor_ObjCProtocolRef:
3168 return createCXString("ObjCProtocolRef");
3169 case CXCursor_ObjCClassRef:
3170 return createCXString("ObjCClassRef");
3171 case CXCursor_TypeRef:
3172 return createCXString("TypeRef");
Douglas Gregor0b36e612010-08-31 20:37:03 +00003173 case CXCursor_TemplateRef:
3174 return createCXString("TemplateRef");
Douglas Gregor69319002010-08-31 23:48:11 +00003175 case CXCursor_NamespaceRef:
3176 return createCXString("NamespaceRef");
Douglas Gregora67e03f2010-09-09 21:42:20 +00003177 case CXCursor_MemberRef:
3178 return createCXString("MemberRef");
Douglas Gregor36897b02010-09-10 00:22:18 +00003179 case CXCursor_LabelRef:
3180 return createCXString("LabelRef");
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003181 case CXCursor_OverloadedDeclRef:
3182 return createCXString("OverloadedDeclRef");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003183 case CXCursor_UnexposedExpr:
3184 return createCXString("UnexposedExpr");
Ted Kremenek1ee6cad2010-04-11 21:47:37 +00003185 case CXCursor_BlockExpr:
3186 return createCXString("BlockExpr");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003187 case CXCursor_DeclRefExpr:
3188 return createCXString("DeclRefExpr");
3189 case CXCursor_MemberRefExpr:
3190 return createCXString("MemberRefExpr");
3191 case CXCursor_CallExpr:
3192 return createCXString("CallExpr");
3193 case CXCursor_ObjCMessageExpr:
3194 return createCXString("ObjCMessageExpr");
3195 case CXCursor_UnexposedStmt:
3196 return createCXString("UnexposedStmt");
Douglas Gregor36897b02010-09-10 00:22:18 +00003197 case CXCursor_LabelStmt:
3198 return createCXString("LabelStmt");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003199 case CXCursor_InvalidFile:
3200 return createCXString("InvalidFile");
Ted Kremenek292db642010-03-19 20:39:05 +00003201 case CXCursor_InvalidCode:
3202 return createCXString("InvalidCode");
Ted Kremeneke68fff62010-02-17 00:41:32 +00003203 case CXCursor_NoDeclFound:
3204 return createCXString("NoDeclFound");
3205 case CXCursor_NotImplemented:
3206 return createCXString("NotImplemented");
3207 case CXCursor_TranslationUnit:
3208 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00003209 case CXCursor_UnexposedAttr:
3210 return createCXString("UnexposedAttr");
3211 case CXCursor_IBActionAttr:
3212 return createCXString("attribute(ibaction)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003213 case CXCursor_IBOutletAttr:
3214 return createCXString("attribute(iboutlet)");
Ted Kremenek857e9182010-05-19 17:38:06 +00003215 case CXCursor_IBOutletCollectionAttr:
3216 return createCXString("attribute(iboutletcollection)");
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003217 case CXCursor_PreprocessingDirective:
3218 return createCXString("preprocessing directive");
Douglas Gregor572feb22010-03-18 18:04:21 +00003219 case CXCursor_MacroDefinition:
3220 return createCXString("macro definition");
Douglas Gregor48072312010-03-18 15:23:44 +00003221 case CXCursor_MacroInstantiation:
3222 return createCXString("macro instantiation");
Douglas Gregorecdcb882010-10-20 22:00:55 +00003223 case CXCursor_InclusionDirective:
3224 return createCXString("inclusion directive");
Ted Kremenek8f06e0e2010-05-06 23:38:21 +00003225 case CXCursor_Namespace:
3226 return createCXString("Namespace");
Ted Kremeneka0536d82010-05-07 01:04:29 +00003227 case CXCursor_LinkageSpec:
3228 return createCXString("LinkageSpec");
Ted Kremenek3064ef92010-08-27 21:34:58 +00003229 case CXCursor_CXXBaseSpecifier:
3230 return createCXString("C++ base class specifier");
Douglas Gregor01829d32010-08-31 14:41:23 +00003231 case CXCursor_Constructor:
3232 return createCXString("CXXConstructor");
3233 case CXCursor_Destructor:
3234 return createCXString("CXXDestructor");
3235 case CXCursor_ConversionFunction:
3236 return createCXString("CXXConversion");
Douglas Gregorfe72e9c2010-08-31 17:01:39 +00003237 case CXCursor_TemplateTypeParameter:
3238 return createCXString("TemplateTypeParameter");
3239 case CXCursor_NonTypeTemplateParameter:
3240 return createCXString("NonTypeTemplateParameter");
3241 case CXCursor_TemplateTemplateParameter:
3242 return createCXString("TemplateTemplateParameter");
3243 case CXCursor_FunctionTemplate:
3244 return createCXString("FunctionTemplate");
Douglas Gregor39d6f072010-08-31 19:02:00 +00003245 case CXCursor_ClassTemplate:
3246 return createCXString("ClassTemplate");
Douglas Gregor74dbe642010-08-31 19:31:58 +00003247 case CXCursor_ClassTemplatePartialSpecialization:
3248 return createCXString("ClassTemplatePartialSpecialization");
Douglas Gregor69319002010-08-31 23:48:11 +00003249 case CXCursor_NamespaceAlias:
3250 return createCXString("NamespaceAlias");
Douglas Gregor0a35bce2010-09-01 03:07:18 +00003251 case CXCursor_UsingDirective:
3252 return createCXString("UsingDirective");
Douglas Gregor7e242562010-09-01 19:52:22 +00003253 case CXCursor_UsingDeclaration:
3254 return createCXString("UsingDeclaration");
Steve Naroff89922f82009-08-31 00:59:03 +00003255 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00003256
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00003257 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneka60ed472010-11-16 08:15:36 +00003258 return createCXString((const char*) 0);
Steve Naroff600866c2009-08-27 19:51:58 +00003259}
Steve Naroff89922f82009-08-31 00:59:03 +00003260
Ted Kremeneke68fff62010-02-17 00:41:32 +00003261enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
3262 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003263 CXClientData client_data) {
3264 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
Douglas Gregor93798e22010-11-05 21:11:19 +00003265
3266 // If our current best cursor is the construction of a temporary object,
3267 // don't replace that cursor with a type reference, because we want
3268 // clang_getCursor() to point at the constructor.
3269 if (clang_isExpression(BestCursor->kind) &&
3270 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
3271 cursor.kind == CXCursor_TypeRef)
3272 return CXChildVisit_Recurse;
3273
Douglas Gregor85fe1562010-12-10 07:23:11 +00003274 // Don't override a preprocessing cursor with another preprocessing
3275 // cursor; we want the outermost preprocessing cursor.
3276 if (clang_isPreprocessing(cursor.kind) &&
3277 clang_isPreprocessing(BestCursor->kind))
3278 return CXChildVisit_Recurse;
3279
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003280 *BestCursor = cursor;
3281 return CXChildVisit_Recurse;
3282}
Ted Kremeneke68fff62010-02-17 00:41:32 +00003283
Douglas Gregorb9790342010-01-22 21:44:22 +00003284CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
3285 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00003286 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00003287
Ted Kremeneka60ed472010-11-16 08:15:36 +00003288 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorbdf60622010-03-05 21:16:25 +00003289 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3290
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003291 // Translate the given source location to make it point at the beginning of
3292 // the token under the cursor.
Ted Kremeneka297de22010-01-25 22:34:44 +00003293 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Ted Kremeneka629ea42010-07-29 00:52:07 +00003294
3295 // Guard against an invalid SourceLocation, or we may assert in one
3296 // of the following calls.
3297 if (SLoc.isInvalid())
3298 return clang_getNullCursor();
3299
Douglas Gregor40749ee2010-11-03 00:35:38 +00003300 bool Logging = getenv("LIBCLANG_LOGGING");
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003301 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
3302 CXXUnit->getASTContext().getLangOptions());
3303
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003304 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
3305 if (SLoc.isValid()) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003306 // FIXME: Would be great to have a "hint" cursor, then walk from that
3307 // hint cursor upward until we find a cursor whose source range encloses
3308 // the region of interest, rather than starting from the translation unit.
Ted Kremeneka60ed472010-11-16 08:15:36 +00003309 CXCursor Parent = clang_getTranslationUnitCursor(TU);
3310 CursorVisitor CursorVis(TU, GetCursorVisitor, &Result,
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003311 Decl::MaxPCHLevel, SourceLocation(SLoc));
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003312 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00003313 }
Douglas Gregor40749ee2010-11-03 00:35:38 +00003314
3315 if (Logging) {
3316 CXFile SearchFile;
3317 unsigned SearchLine, SearchColumn;
3318 CXFile ResultFile;
3319 unsigned ResultLine, ResultColumn;
Douglas Gregor66537982010-11-17 17:14:07 +00003320 CXString SearchFileName, ResultFileName, KindSpelling, USR;
3321 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
Douglas Gregor40749ee2010-11-03 00:35:38 +00003322 CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
3323
3324 clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
3325 0);
3326 clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine,
3327 &ResultColumn, 0);
3328 SearchFileName = clang_getFileName(SearchFile);
3329 ResultFileName = clang_getFileName(ResultFile);
3330 KindSpelling = clang_getCursorKindSpelling(Result.kind);
Douglas Gregor66537982010-11-17 17:14:07 +00003331 USR = clang_getCursorUSR(Result);
3332 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n",
Douglas Gregor40749ee2010-11-03 00:35:38 +00003333 clang_getCString(SearchFileName), SearchLine, SearchColumn,
3334 clang_getCString(KindSpelling),
Douglas Gregor66537982010-11-17 17:14:07 +00003335 clang_getCString(ResultFileName), ResultLine, ResultColumn,
3336 clang_getCString(USR), IsDef);
Douglas Gregor40749ee2010-11-03 00:35:38 +00003337 clang_disposeString(SearchFileName);
3338 clang_disposeString(ResultFileName);
3339 clang_disposeString(KindSpelling);
Douglas Gregor66537982010-11-17 17:14:07 +00003340 clang_disposeString(USR);
Douglas Gregor0aefbd82010-12-10 01:45:00 +00003341
3342 CXCursor Definition = clang_getCursorDefinition(Result);
3343 if (!clang_equalCursors(Definition, clang_getNullCursor())) {
3344 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
3345 CXString DefinitionKindSpelling
3346 = clang_getCursorKindSpelling(Definition.kind);
3347 CXFile DefinitionFile;
3348 unsigned DefinitionLine, DefinitionColumn;
3349 clang_getInstantiationLocation(DefinitionLoc, &DefinitionFile,
3350 &DefinitionLine, &DefinitionColumn, 0);
3351 CXString DefinitionFileName = clang_getFileName(DefinitionFile);
3352 fprintf(stderr, " -> %s(%s:%d:%d)\n",
3353 clang_getCString(DefinitionKindSpelling),
3354 clang_getCString(DefinitionFileName),
3355 DefinitionLine, DefinitionColumn);
3356 clang_disposeString(DefinitionFileName);
3357 clang_disposeString(DefinitionKindSpelling);
3358 }
Douglas Gregor40749ee2010-11-03 00:35:38 +00003359 }
3360
Ted Kremeneke68fff62010-02-17 00:41:32 +00003361 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00003362}
3363
Ted Kremenek73885552009-11-17 19:28:59 +00003364CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00003365 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00003366}
3367
3368unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00003369 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00003370}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00003371
Douglas Gregor9ce55842010-11-20 00:09:34 +00003372unsigned clang_hashCursor(CXCursor C) {
3373 unsigned Index = 0;
3374 if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
3375 Index = 1;
3376
3377 return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue(
3378 std::make_pair(C.kind, C.data[Index]));
3379}
3380
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003381unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00003382 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
3383}
3384
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003385unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00003386 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
3387}
Steve Naroff2d4d6292009-08-31 14:26:51 +00003388
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003389unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00003390 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
3391}
3392
Douglas Gregor97b98722010-01-19 23:20:36 +00003393unsigned clang_isExpression(enum CXCursorKind K) {
3394 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
3395}
3396
3397unsigned clang_isStatement(enum CXCursorKind K) {
3398 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
3399}
3400
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00003401unsigned clang_isTranslationUnit(enum CXCursorKind K) {
3402 return K == CXCursor_TranslationUnit;
3403}
3404
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003405unsigned clang_isPreprocessing(enum CXCursorKind K) {
3406 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
3407}
3408
Ted Kremenekad6eff62010-03-08 21:17:29 +00003409unsigned clang_isUnexposed(enum CXCursorKind K) {
3410 switch (K) {
3411 case CXCursor_UnexposedDecl:
3412 case CXCursor_UnexposedExpr:
3413 case CXCursor_UnexposedStmt:
3414 case CXCursor_UnexposedAttr:
3415 return true;
3416 default:
3417 return false;
3418 }
3419}
3420
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00003421CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00003422 return C.kind;
3423}
3424
Douglas Gregor98258af2010-01-18 22:46:11 +00003425CXSourceLocation clang_getCursorLocation(CXCursor C) {
3426 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003427 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003428 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003429 std::pair<ObjCInterfaceDecl *, SourceLocation> P
3430 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003431 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003432 }
3433
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003434 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003435 std::pair<ObjCProtocolDecl *, SourceLocation> P
3436 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003437 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003438 }
3439
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003440 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00003441 std::pair<ObjCInterfaceDecl *, SourceLocation> P
3442 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003443 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003444 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003445
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003446 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003447 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00003448 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003449 }
Douglas Gregor0b36e612010-08-31 20:37:03 +00003450
3451 case CXCursor_TemplateRef: {
3452 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C);
3453 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3454 }
3455
Douglas Gregor69319002010-08-31 23:48:11 +00003456 case CXCursor_NamespaceRef: {
3457 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
3458 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3459 }
3460
Douglas Gregora67e03f2010-09-09 21:42:20 +00003461 case CXCursor_MemberRef: {
3462 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
3463 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3464 }
3465
Ted Kremenek3064ef92010-08-27 21:34:58 +00003466 case CXCursor_CXXBaseSpecifier: {
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003467 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
3468 if (!BaseSpec)
3469 return clang_getNullLocation();
3470
3471 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
3472 return cxloc::translateSourceLocation(getCursorContext(C),
3473 TSInfo->getTypeLoc().getBeginLoc());
3474
3475 return cxloc::translateSourceLocation(getCursorContext(C),
3476 BaseSpec->getSourceRange().getBegin());
Ted Kremenek3064ef92010-08-27 21:34:58 +00003477 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003478
Douglas Gregor36897b02010-09-10 00:22:18 +00003479 case CXCursor_LabelRef: {
3480 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
3481 return cxloc::translateSourceLocation(getCursorContext(C), P.second);
3482 }
3483
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003484 case CXCursor_OverloadedDeclRef:
3485 return cxloc::translateSourceLocation(getCursorContext(C),
3486 getCursorOverloadedDeclRef(C).second);
3487
Douglas Gregorf46034a2010-01-18 23:41:10 +00003488 default:
3489 // FIXME: Need a way to enumerate all non-reference cases.
3490 llvm_unreachable("Missed a reference kind");
3491 }
Douglas Gregor98258af2010-01-18 22:46:11 +00003492 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003493
3494 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003495 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00003496 getLocationFromExpr(getCursorExpr(C)));
3497
Douglas Gregor36897b02010-09-10 00:22:18 +00003498 if (clang_isStatement(C.kind))
3499 return cxloc::translateSourceLocation(getCursorContext(C),
3500 getCursorStmt(C)->getLocStart());
3501
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00003502 if (C.kind == CXCursor_PreprocessingDirective) {
3503 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
3504 return cxloc::translateSourceLocation(getCursorContext(C), L);
3505 }
Douglas Gregor48072312010-03-18 15:23:44 +00003506
3507 if (C.kind == CXCursor_MacroInstantiation) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +00003508 SourceLocation L
3509 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
Douglas Gregor48072312010-03-18 15:23:44 +00003510 return cxloc::translateSourceLocation(getCursorContext(C), L);
3511 }
Douglas Gregor572feb22010-03-18 18:04:21 +00003512
3513 if (C.kind == CXCursor_MacroDefinition) {
3514 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
3515 return cxloc::translateSourceLocation(getCursorContext(C), L);
3516 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00003517
3518 if (C.kind == CXCursor_InclusionDirective) {
3519 SourceLocation L
3520 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
3521 return cxloc::translateSourceLocation(getCursorContext(C), L);
3522 }
3523
Ted Kremenek9a700d22010-05-12 06:16:13 +00003524 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
Douglas Gregor5352ac02010-01-28 00:27:43 +00003525 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00003526
Douglas Gregorf46034a2010-01-18 23:41:10 +00003527 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00003528 SourceLocation Loc = D->getLocation();
3529 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
3530 Loc = Class->getClassLoc();
Ted Kremenek007a7c92010-11-01 23:26:51 +00003531 // FIXME: Multiple variables declared in a single declaration
3532 // currently lack the information needed to correctly determine their
3533 // ranges when accounting for the type-specifier. We use context
3534 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3535 // and if so, whether it is the first decl.
3536 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3537 if (!cxcursor::isFirstInDeclGroup(C))
3538 Loc = VD->getLocation();
3539 }
3540
Douglas Gregor2ca54fe2010-03-22 15:53:50 +00003541 return cxloc::translateSourceLocation(getCursorContext(C), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00003542}
Douglas Gregora7bde202010-01-19 00:34:46 +00003543
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003544} // end extern "C"
3545
3546static SourceRange getRawCursorExtent(CXCursor C) {
Douglas Gregora7bde202010-01-19 00:34:46 +00003547 if (clang_isReference(C.kind)) {
3548 switch (C.kind) {
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003549 case CXCursor_ObjCSuperClassRef:
3550 return getCursorObjCSuperClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003551
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003552 case CXCursor_ObjCProtocolRef:
3553 return getCursorObjCProtocolRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003554
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003555 case CXCursor_ObjCClassRef:
3556 return getCursorObjCClassRef(C).second;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003557
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003558 case CXCursor_TypeRef:
3559 return getCursorTypeRef(C).second;
Douglas Gregor0b36e612010-08-31 20:37:03 +00003560
3561 case CXCursor_TemplateRef:
3562 return getCursorTemplateRef(C).second;
3563
Douglas Gregor69319002010-08-31 23:48:11 +00003564 case CXCursor_NamespaceRef:
3565 return getCursorNamespaceRef(C).second;
Douglas Gregora67e03f2010-09-09 21:42:20 +00003566
3567 case CXCursor_MemberRef:
3568 return getCursorMemberRef(C).second;
3569
Ted Kremenek3064ef92010-08-27 21:34:58 +00003570 case CXCursor_CXXBaseSpecifier:
Douglas Gregor1b0f7af2010-10-02 19:51:13 +00003571 return getCursorCXXBaseSpecifier(C)->getSourceRange();
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003572
Douglas Gregor36897b02010-09-10 00:22:18 +00003573 case CXCursor_LabelRef:
3574 return getCursorLabelRef(C).second;
3575
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003576 case CXCursor_OverloadedDeclRef:
3577 return getCursorOverloadedDeclRef(C).second;
3578
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003579 default:
3580 // FIXME: Need a way to enumerate all non-reference cases.
3581 llvm_unreachable("Missed a reference kind");
Douglas Gregora7bde202010-01-19 00:34:46 +00003582 }
3583 }
Douglas Gregor97b98722010-01-19 23:20:36 +00003584
3585 if (clang_isExpression(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003586 return getCursorExpr(C)->getSourceRange();
Douglas Gregor33e9abd2010-01-22 19:49:59 +00003587
3588 if (clang_isStatement(C.kind))
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003589 return getCursorStmt(C)->getSourceRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003590
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003591 if (C.kind == CXCursor_PreprocessingDirective)
3592 return cxcursor::getCursorPreprocessingDirective(C);
Douglas Gregor48072312010-03-18 15:23:44 +00003593
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003594 if (C.kind == CXCursor_MacroInstantiation)
3595 return cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
Douglas Gregor572feb22010-03-18 18:04:21 +00003596
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003597 if (C.kind == CXCursor_MacroDefinition)
3598 return cxcursor::getCursorMacroDefinition(C)->getSourceRange();
Douglas Gregorecdcb882010-10-20 22:00:55 +00003599
3600 if (C.kind == CXCursor_InclusionDirective)
3601 return cxcursor::getCursorInclusionDirective(C)->getSourceRange();
3602
Ted Kremenek007a7c92010-11-01 23:26:51 +00003603 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3604 Decl *D = cxcursor::getCursorDecl(C);
3605 SourceRange R = D->getSourceRange();
3606 // FIXME: Multiple variables declared in a single declaration
3607 // currently lack the information needed to correctly determine their
3608 // ranges when accounting for the type-specifier. We use context
3609 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3610 // and if so, whether it is the first decl.
3611 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3612 if (!cxcursor::isFirstInDeclGroup(C))
3613 R.setBegin(VD->getLocation());
3614 }
3615 return R;
3616 }
Douglas Gregor66537982010-11-17 17:14:07 +00003617 return SourceRange();
3618}
3619
3620/// \brief Retrieves the "raw" cursor extent, which is then extended to include
3621/// the decl-specifier-seq for declarations.
3622static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
3623 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3624 Decl *D = cxcursor::getCursorDecl(C);
3625 SourceRange R = D->getSourceRange();
Douglas Gregor66537982010-11-17 17:14:07 +00003626
Douglas Gregor2494dd02011-03-01 01:34:45 +00003627 // Adjust the start of the location for declarations preceded by
3628 // declaration specifiers.
3629 SourceLocation StartLoc;
3630 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
3631 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
3632 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3633 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
3634 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
3635 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3636 }
3637
3638 if (StartLoc.isValid() && R.getBegin().isValid() &&
3639 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
3640 R.setBegin(StartLoc);
3641
3642 // FIXME: Multiple variables declared in a single declaration
3643 // currently lack the information needed to correctly determine their
3644 // ranges when accounting for the type-specifier. We use context
3645 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3646 // and if so, whether it is the first decl.
3647 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3648 if (!cxcursor::isFirstInDeclGroup(C))
3649 R.setBegin(VD->getLocation());
Douglas Gregor66537982010-11-17 17:14:07 +00003650 }
3651
3652 return R;
3653 }
3654
3655 return getRawCursorExtent(C);
3656}
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003657
3658extern "C" {
3659
3660CXSourceRange clang_getCursorExtent(CXCursor C) {
3661 SourceRange R = getRawCursorExtent(C);
3662 if (R.isInvalid())
Douglas Gregor5352ac02010-01-28 00:27:43 +00003663 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003664
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00003665 return cxloc::translateSourceRange(getCursorContext(C), R);
Douglas Gregora7bde202010-01-19 00:34:46 +00003666}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003667
3668CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003669 if (clang_isInvalid(C.kind))
3670 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003671
Ted Kremeneka60ed472010-11-16 08:15:36 +00003672 CXTranslationUnit tu = getCursorTU(C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003673 if (clang_isDeclaration(C.kind)) {
3674 Decl *D = getCursorDecl(C);
3675 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003676 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003677 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003678 return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003679 if (ObjCForwardProtocolDecl *Protocols
3680 = dyn_cast<ObjCForwardProtocolDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003681 return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu);
Douglas Gregore3c60a72010-11-17 00:13:31 +00003682 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
3683 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
3684 return MakeCXCursor(Property, tu);
3685
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003686 return C;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003687 }
3688
Douglas Gregor97b98722010-01-19 23:20:36 +00003689 if (clang_isExpression(C.kind)) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003690 Expr *E = getCursorExpr(C);
3691 Decl *D = getDeclFromExpr(E);
Douglas Gregor97b98722010-01-19 23:20:36 +00003692 if (D)
Ted Kremeneka60ed472010-11-16 08:15:36 +00003693 return MakeCXCursor(D, tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003694
3695 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003696 return MakeCursorOverloadedDeclRef(Ovl, tu);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003697
Douglas Gregor97b98722010-01-19 23:20:36 +00003698 return clang_getNullCursor();
3699 }
3700
Douglas Gregor36897b02010-09-10 00:22:18 +00003701 if (clang_isStatement(C.kind)) {
3702 Stmt *S = getCursorStmt(C);
3703 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003704 return MakeCXCursor(Goto->getLabel()->getStmt(), getCursorDecl(C), tu);
Douglas Gregor36897b02010-09-10 00:22:18 +00003705
3706 return clang_getNullCursor();
3707 }
3708
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003709 if (C.kind == CXCursor_MacroInstantiation) {
3710 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003711 return MakeMacroDefinitionCursor(Def, tu);
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003712 }
3713
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003714 if (!clang_isReference(C.kind))
3715 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003716
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003717 switch (C.kind) {
3718 case CXCursor_ObjCSuperClassRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003719 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003720
3721 case CXCursor_ObjCProtocolRef: {
Ted Kremeneka60ed472010-11-16 08:15:36 +00003722 return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003723
3724 case CXCursor_ObjCClassRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003725 return MakeCXCursor(getCursorObjCClassRef(C).first, tu );
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00003726
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003727 case CXCursor_TypeRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003728 return MakeCXCursor(getCursorTypeRef(C).first, tu );
Douglas Gregor0b36e612010-08-31 20:37:03 +00003729
3730 case CXCursor_TemplateRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003731 return MakeCXCursor(getCursorTemplateRef(C).first, tu );
Douglas Gregor0b36e612010-08-31 20:37:03 +00003732
Douglas Gregor69319002010-08-31 23:48:11 +00003733 case CXCursor_NamespaceRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003734 return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
Douglas Gregor69319002010-08-31 23:48:11 +00003735
Douglas Gregora67e03f2010-09-09 21:42:20 +00003736 case CXCursor_MemberRef:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003737 return MakeCXCursor(getCursorMemberRef(C).first, tu );
Douglas Gregora67e03f2010-09-09 21:42:20 +00003738
Ted Kremenek3064ef92010-08-27 21:34:58 +00003739 case CXCursor_CXXBaseSpecifier: {
3740 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
3741 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003742 tu ));
Ted Kremenek3064ef92010-08-27 21:34:58 +00003743 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003744
Douglas Gregor36897b02010-09-10 00:22:18 +00003745 case CXCursor_LabelRef:
3746 // FIXME: We end up faking the "parent" declaration here because we
3747 // don't want to make CXCursor larger.
3748 return MakeCXCursor(getCursorLabelRef(C).first,
Ted Kremeneka60ed472010-11-16 08:15:36 +00003749 static_cast<ASTUnit*>(tu->TUData)->getASTContext()
3750 .getTranslationUnitDecl(),
3751 tu);
Douglas Gregor36897b02010-09-10 00:22:18 +00003752
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003753 case CXCursor_OverloadedDeclRef:
3754 return C;
3755
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003756 default:
3757 // We would prefer to enumerate all non-reference cursor kinds here.
3758 llvm_unreachable("Unhandled reference cursor kind");
3759 break;
3760 }
3761 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003762
Douglas Gregorc5d1e932010-01-19 01:20:04 +00003763 return clang_getNullCursor();
3764}
3765
Douglas Gregorb6998662010-01-19 19:34:47 +00003766CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003767 if (clang_isInvalid(C.kind))
3768 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003769
Ted Kremeneka60ed472010-11-16 08:15:36 +00003770 CXTranslationUnit TU = getCursorTU(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003771
Douglas Gregorb6998662010-01-19 19:34:47 +00003772 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00003773 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00003774 C = clang_getCursorReferenced(C);
3775 WasReference = true;
3776 }
3777
Douglas Gregorbf7efa22010-03-18 18:23:03 +00003778 if (C.kind == CXCursor_MacroInstantiation)
3779 return clang_getCursorReferenced(C);
3780
Douglas Gregorb6998662010-01-19 19:34:47 +00003781 if (!clang_isDeclaration(C.kind))
3782 return clang_getNullCursor();
3783
3784 Decl *D = getCursorDecl(C);
3785 if (!D)
3786 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003787
Douglas Gregorb6998662010-01-19 19:34:47 +00003788 switch (D->getKind()) {
3789 // Declaration kinds that don't really separate the notions of
3790 // declaration and definition.
3791 case Decl::Namespace:
3792 case Decl::Typedef:
3793 case Decl::TemplateTypeParm:
3794 case Decl::EnumConstant:
3795 case Decl::Field:
Benjamin Kramerd9811462010-11-21 14:11:41 +00003796 case Decl::IndirectField:
Douglas Gregorb6998662010-01-19 19:34:47 +00003797 case Decl::ObjCIvar:
3798 case Decl::ObjCAtDefsField:
3799 case Decl::ImplicitParam:
3800 case Decl::ParmVar:
3801 case Decl::NonTypeTemplateParm:
3802 case Decl::TemplateTemplateParm:
3803 case Decl::ObjCCategoryImpl:
3804 case Decl::ObjCImplementation:
Abramo Bagnara6206d532010-06-05 05:09:32 +00003805 case Decl::AccessSpec:
Douglas Gregorb6998662010-01-19 19:34:47 +00003806 case Decl::LinkageSpec:
3807 case Decl::ObjCPropertyImpl:
3808 case Decl::FileScopeAsm:
3809 case Decl::StaticAssert:
3810 case Decl::Block:
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003811 case Decl::Label: // FIXME: Is this right??
Douglas Gregorb6998662010-01-19 19:34:47 +00003812 return C;
3813
3814 // Declaration kinds that don't make any sense here, but are
3815 // nonetheless harmless.
3816 case Decl::TranslationUnit:
Douglas Gregorb6998662010-01-19 19:34:47 +00003817 break;
3818
3819 // Declaration kinds for which the definition is not resolvable.
3820 case Decl::UnresolvedUsingTypename:
3821 case Decl::UnresolvedUsingValue:
3822 break;
3823
3824 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00003825 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003826 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003827
3828 case Decl::NamespaceAlias:
Ted Kremeneka60ed472010-11-16 08:15:36 +00003829 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003830
3831 case Decl::Enum:
3832 case Decl::Record:
3833 case Decl::CXXRecord:
3834 case Decl::ClassTemplateSpecialization:
3835 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00003836 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003837 return MakeCXCursor(Def, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003838 return clang_getNullCursor();
3839
3840 case Decl::Function:
3841 case Decl::CXXMethod:
3842 case Decl::CXXConstructor:
3843 case Decl::CXXDestructor:
3844 case Decl::CXXConversion: {
3845 const FunctionDecl *Def = 0;
3846 if (cast<FunctionDecl>(D)->getBody(Def))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003847 return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003848 return clang_getNullCursor();
3849 }
3850
3851 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00003852 // Ask the variable if it has a definition.
3853 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003854 return MakeCXCursor(Def, TU);
Sebastian Redl31310a22010-02-01 20:16:42 +00003855 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00003856 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003857
Douglas Gregorb6998662010-01-19 19:34:47 +00003858 case Decl::FunctionTemplate: {
3859 const FunctionDecl *Def = 0;
3860 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Ted Kremeneka60ed472010-11-16 08:15:36 +00003861 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003862 return clang_getNullCursor();
3863 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003864
Douglas Gregorb6998662010-01-19 19:34:47 +00003865 case Decl::ClassTemplate: {
3866 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00003867 ->getDefinition())
Douglas Gregor0b36e612010-08-31 20:37:03 +00003868 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003869 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003870 return clang_getNullCursor();
3871 }
3872
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003873 case Decl::Using:
3874 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003875 D->getLocation(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003876
3877 case Decl::UsingShadow:
3878 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003879 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003880 TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003881
3882 case Decl::ObjCMethod: {
3883 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
3884 if (Method->isThisDeclarationADefinition())
3885 return C;
3886
3887 // Dig out the method definition in the associated
3888 // @implementation, if we have it.
3889 // FIXME: The ASTs should make finding the definition easier.
3890 if (ObjCInterfaceDecl *Class
3891 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
3892 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
3893 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
3894 Method->isInstanceMethod()))
3895 if (Def->isThisDeclarationADefinition())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003896 return MakeCXCursor(Def, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003897
3898 return clang_getNullCursor();
3899 }
3900
3901 case Decl::ObjCCategory:
3902 if (ObjCCategoryImplDecl *Impl
3903 = cast<ObjCCategoryDecl>(D)->getImplementation())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003904 return MakeCXCursor(Impl, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003905 return clang_getNullCursor();
3906
3907 case Decl::ObjCProtocol:
3908 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
3909 return C;
3910 return clang_getNullCursor();
3911
3912 case Decl::ObjCInterface:
3913 // There are two notions of a "definition" for an Objective-C
3914 // class: the interface and its implementation. When we resolved a
3915 // reference to an Objective-C class, produce the @interface as
3916 // the definition; when we were provided with the interface,
3917 // produce the @implementation as the definition.
3918 if (WasReference) {
3919 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
3920 return C;
3921 } else if (ObjCImplementationDecl *Impl
3922 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003923 return MakeCXCursor(Impl, TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003924 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003925
Douglas Gregorb6998662010-01-19 19:34:47 +00003926 case Decl::ObjCProperty:
3927 // FIXME: We don't really know where to find the
3928 // ObjCPropertyImplDecls that implement this property.
3929 return clang_getNullCursor();
3930
3931 case Decl::ObjCCompatibleAlias:
3932 if (ObjCInterfaceDecl *Class
3933 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
3934 if (!Class->isForwardDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003935 return MakeCXCursor(Class, TU);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00003936
Douglas Gregorb6998662010-01-19 19:34:47 +00003937 return clang_getNullCursor();
3938
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003939 case Decl::ObjCForwardProtocol:
3940 return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003941 D->getLocation(), TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003942
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003943 case Decl::ObjCClass:
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00003944 return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(),
Ted Kremeneka60ed472010-11-16 08:15:36 +00003945 TU);
Douglas Gregorb6998662010-01-19 19:34:47 +00003946
3947 case Decl::Friend:
3948 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003949 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003950 return clang_getNullCursor();
3951
3952 case Decl::FriendTemplate:
3953 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Ted Kremeneka60ed472010-11-16 08:15:36 +00003954 return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
Douglas Gregorb6998662010-01-19 19:34:47 +00003955 return clang_getNullCursor();
3956 }
3957
3958 return clang_getNullCursor();
3959}
3960
3961unsigned clang_isCursorDefinition(CXCursor C) {
3962 if (!clang_isDeclaration(C.kind))
3963 return 0;
3964
3965 return clang_getCursorDefinition(C) == C;
3966}
3967
Douglas Gregor1a9d0502010-11-19 23:44:15 +00003968CXCursor clang_getCanonicalCursor(CXCursor C) {
3969 if (!clang_isDeclaration(C.kind))
3970 return C;
3971
3972 if (Decl *D = getCursorDecl(C))
3973 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
3974
3975 return C;
3976}
3977
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003978unsigned clang_getNumOverloadedDecls(CXCursor C) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00003979 if (C.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003980 return 0;
3981
3982 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
3983 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
3984 return E->getNumDecls();
3985
3986 if (OverloadedTemplateStorage *S
3987 = Storage.dyn_cast<OverloadedTemplateStorage*>())
3988 return S->size();
3989
3990 Decl *D = Storage.get<Decl*>();
3991 if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00003992 return Using->shadow_size();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00003993 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
3994 return Classes->size();
3995 if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D))
3996 return Protocols->protocol_size();
3997
3998 return 0;
3999}
4000
4001CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
Douglas Gregor7c432dd2010-09-16 13:54:00 +00004002 if (cursor.kind != CXCursor_OverloadedDeclRef)
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004003 return clang_getNullCursor();
4004
4005 if (index >= clang_getNumOverloadedDecls(cursor))
4006 return clang_getNullCursor();
4007
Ted Kremeneka60ed472010-11-16 08:15:36 +00004008 CXTranslationUnit TU = getCursorTU(cursor);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004009 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
4010 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
Ted Kremeneka60ed472010-11-16 08:15:36 +00004011 return MakeCXCursor(E->decls_begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004012
4013 if (OverloadedTemplateStorage *S
4014 = Storage.dyn_cast<OverloadedTemplateStorage*>())
Ted Kremeneka60ed472010-11-16 08:15:36 +00004015 return MakeCXCursor(S->begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004016
4017 Decl *D = Storage.get<Decl*>();
4018 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
4019 // FIXME: This is, unfortunately, linear time.
4020 UsingDecl::shadow_iterator Pos = Using->shadow_begin();
4021 std::advance(Pos, index);
Ted Kremeneka60ed472010-11-16 08:15:36 +00004022 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004023 }
4024
4025 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004026 return MakeCXCursor(Classes->begin()[index].getInterface(), TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004027
4028 if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004029 return MakeCXCursor(Protocols->protocol_begin()[index], TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +00004030
4031 return clang_getNullCursor();
4032}
4033
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00004034void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00004035 const char **startBuf,
4036 const char **endBuf,
4037 unsigned *startLine,
4038 unsigned *startColumn,
4039 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00004040 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00004041 assert(getCursorDecl(C) && "CXCursor has null decl");
4042 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00004043 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
4044 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004045
Steve Naroff4ade6d62009-09-23 17:52:52 +00004046 SourceManager &SM = FD->getASTContext().getSourceManager();
4047 *startBuf = SM.getCharacterData(Body->getLBracLoc());
4048 *endBuf = SM.getCharacterData(Body->getRBracLoc());
4049 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
4050 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
4051 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
4052 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
4053}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004054
Douglas Gregor0a812cf2010-02-18 23:07:20 +00004055void clang_enableStackTraces(void) {
4056 llvm::sys::PrintStackTraceOnErrorSignal();
4057}
4058
Daniel Dunbar995aaf92010-11-04 01:26:29 +00004059void clang_executeOnThread(void (*fn)(void*), void *user_data,
4060 unsigned stack_size) {
4061 llvm::llvm_execute_on_thread(fn, user_data, stack_size);
4062}
4063
Ted Kremenekfb480492010-01-13 21:46:36 +00004064} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00004065
Ted Kremenekfb480492010-01-13 21:46:36 +00004066//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004067// Token-based Operations.
4068//===----------------------------------------------------------------------===//
4069
4070/* CXToken layout:
4071 * int_data[0]: a CXTokenKind
4072 * int_data[1]: starting token location
4073 * int_data[2]: token length
4074 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004075 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004076 * otherwise unused.
4077 */
4078extern "C" {
4079
4080CXTokenKind clang_getTokenKind(CXToken CXTok) {
4081 return static_cast<CXTokenKind>(CXTok.int_data[0]);
4082}
4083
4084CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
4085 switch (clang_getTokenKind(CXTok)) {
4086 case CXToken_Identifier:
4087 case CXToken_Keyword:
4088 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004089 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
4090 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004091
4092 case CXToken_Literal: {
4093 // We have stashed the starting pointer in the ptr_data field. Use it.
4094 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004095 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004096 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004097
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004098 case CXToken_Punctuation:
4099 case CXToken_Comment:
4100 break;
4101 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004102
4103 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004104 // deconstructing the source location.
Ted Kremeneka60ed472010-11-16 08:15:36 +00004105 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004106 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004107 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004108
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004109 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
4110 std::pair<FileID, unsigned> LocInfo
4111 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +00004112 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004113 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004114 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
4115 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +00004116 return createCXString("");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004117
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004118 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004119}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004120
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004121CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004122 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004123 if (!CXXUnit)
4124 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004125
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004126 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
4127 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4128}
4129
4130CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004131 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor5352ac02010-01-28 00:27:43 +00004132 if (!CXXUnit)
4133 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004134
4135 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004136 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4137}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004138
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004139void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
4140 CXToken **Tokens, unsigned *NumTokens) {
4141 if (Tokens)
4142 *Tokens = 0;
4143 if (NumTokens)
4144 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004145
Ted Kremeneka60ed472010-11-16 08:15:36 +00004146 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004147 if (!CXXUnit || !Tokens || !NumTokens)
4148 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004149
Douglas Gregorbdf60622010-03-05 21:16:25 +00004150 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4151
Daniel Dunbar85b988f2010-02-14 08:31:57 +00004152 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004153 if (R.isInvalid())
4154 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004155
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004156 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4157 std::pair<FileID, unsigned> BeginLocInfo
4158 = SourceMgr.getDecomposedLoc(R.getBegin());
4159 std::pair<FileID, unsigned> EndLocInfo
4160 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004161
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004162 // Cannot tokenize across files.
4163 if (BeginLocInfo.first != EndLocInfo.first)
4164 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004165
4166 // Create a lexer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004167 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004168 llvm::StringRef Buffer
Douglas Gregorf715ca12010-03-16 00:06:06 +00004169 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
Douglas Gregor47a3fcd2010-03-16 20:26:15 +00004170 if (Invalid)
4171 return;
Douglas Gregoraea67db2010-03-15 22:54:52 +00004172
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004173 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4174 CXXUnit->getASTContext().getLangOptions(),
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004175 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004176 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004177
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004178 // Lex tokens until we hit the end of the range.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +00004179 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004180 llvm::SmallVector<CXToken, 32> CXTokens;
4181 Token Tok;
David Chisnall096428b2010-10-13 21:44:48 +00004182 bool previousWasAt = false;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004183 do {
4184 // Lex the next token
4185 Lex.LexFromRawLexer(Tok);
4186 if (Tok.is(tok::eof))
4187 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004188
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004189 // Initialize the CXToken.
4190 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004191
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004192 // - Common fields
4193 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
4194 CXTok.int_data[2] = Tok.getLength();
4195 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004196
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004197 // - Kind-specific fields
4198 if (Tok.isLiteral()) {
4199 CXTok.int_data[0] = CXToken_Literal;
4200 CXTok.ptr_data = (void *)Tok.getLiteralData();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004201 } else if (Tok.is(tok::raw_identifier)) {
Douglas Gregoraea67db2010-03-15 22:54:52 +00004202 // Lookup the identifier to determine whether we have a keyword.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004203 IdentifierInfo *II
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004204 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004205
David Chisnall096428b2010-10-13 21:44:48 +00004206 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004207 CXTok.int_data[0] = CXToken_Keyword;
4208 }
4209 else {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +00004210 CXTok.int_data[0] = Tok.is(tok::identifier)
4211 ? CXToken_Identifier
4212 : CXToken_Keyword;
Ted Kremenekaa8a66d2010-05-05 00:55:20 +00004213 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004214 CXTok.ptr_data = II;
4215 } else if (Tok.is(tok::comment)) {
4216 CXTok.int_data[0] = CXToken_Comment;
4217 CXTok.ptr_data = 0;
4218 } else {
4219 CXTok.int_data[0] = CXToken_Punctuation;
4220 CXTok.ptr_data = 0;
4221 }
4222 CXTokens.push_back(CXTok);
David Chisnall096428b2010-10-13 21:44:48 +00004223 previousWasAt = Tok.is(tok::at);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004224 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004225
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004226 if (CXTokens.empty())
4227 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004228
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004229 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
4230 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
4231 *NumTokens = CXTokens.size();
4232}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004233
Ted Kremenek6db61092010-05-05 00:55:15 +00004234void clang_disposeTokens(CXTranslationUnit TU,
4235 CXToken *Tokens, unsigned NumTokens) {
4236 free(Tokens);
4237}
4238
4239} // end: extern "C"
4240
4241//===----------------------------------------------------------------------===//
4242// Token annotation APIs.
4243//===----------------------------------------------------------------------===//
4244
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004245typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004246static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4247 CXCursor parent,
4248 CXClientData client_data);
Ted Kremenek6db61092010-05-05 00:55:15 +00004249namespace {
4250class AnnotateTokensWorker {
4251 AnnotateTokensData &Annotated;
Ted Kremenek11949cb2010-05-05 00:55:17 +00004252 CXToken *Tokens;
4253 CXCursor *Cursors;
4254 unsigned NumTokens;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004255 unsigned TokIdx;
Douglas Gregor4419b672010-10-21 06:10:04 +00004256 unsigned PreprocessingTokIdx;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004257 CursorVisitor AnnotateVis;
4258 SourceManager &SrcMgr;
4259
4260 bool MoreTokens() const { return TokIdx < NumTokens; }
4261 unsigned NextToken() const { return TokIdx; }
4262 void AdvanceToken() { ++TokIdx; }
4263 SourceLocation GetTokenLoc(unsigned tokI) {
4264 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
4265 }
4266
Ted Kremenek6db61092010-05-05 00:55:15 +00004267public:
Ted Kremenek11949cb2010-05-05 00:55:17 +00004268 AnnotateTokensWorker(AnnotateTokensData &annotated,
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004269 CXToken *tokens, CXCursor *cursors, unsigned numTokens,
Ted Kremeneka60ed472010-11-16 08:15:36 +00004270 CXTranslationUnit tu, SourceRange RegionOfInterest)
Ted Kremenek11949cb2010-05-05 00:55:17 +00004271 : Annotated(annotated), Tokens(tokens), Cursors(cursors),
Douglas Gregor4419b672010-10-21 06:10:04 +00004272 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004273 AnnotateVis(tu,
4274 AnnotateTokensVisitor, this,
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004275 Decl::MaxPCHLevel, RegionOfInterest),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004276 SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()) {}
Ted Kremenek11949cb2010-05-05 00:55:17 +00004277
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004278 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
Ted Kremenek6db61092010-05-05 00:55:15 +00004279 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004280 void AnnotateTokens(CXCursor parent);
Ted Kremenekab979612010-11-11 08:05:23 +00004281 void AnnotateTokens() {
Ted Kremeneka60ed472010-11-16 08:15:36 +00004282 AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU()));
Ted Kremenekab979612010-11-11 08:05:23 +00004283 }
Ted Kremenek6db61092010-05-05 00:55:15 +00004284};
4285}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004286
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004287void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) {
4288 // Walk the AST within the region of interest, annotating tokens
4289 // along the way.
4290 VisitChildren(parent);
Ted Kremenek11949cb2010-05-05 00:55:17 +00004291
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004292 for (unsigned I = 0 ; I < TokIdx ; ++I) {
4293 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
Douglas Gregor4419b672010-10-21 06:10:04 +00004294 if (Pos != Annotated.end() &&
4295 (clang_isInvalid(Cursors[I].kind) ||
4296 Pos->second.kind != CXCursor_PreprocessingDirective))
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004297 Cursors[I] = Pos->second;
4298 }
4299
4300 // Finish up annotating any tokens left.
4301 if (!MoreTokens())
4302 return;
4303
4304 const CXCursor &C = clang_getNullCursor();
4305 for (unsigned I = TokIdx ; I < NumTokens ; ++I) {
4306 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
4307 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second;
Ted Kremenek11949cb2010-05-05 00:55:17 +00004308 }
4309}
4310
Ted Kremenek6db61092010-05-05 00:55:15 +00004311enum CXChildVisitResult
Douglas Gregor4419b672010-10-21 06:10:04 +00004312AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004313 CXSourceLocation Loc = clang_getCursorLocation(cursor);
Douglas Gregor4419b672010-10-21 06:10:04 +00004314 SourceRange cursorRange = getRawCursorExtent(cursor);
Douglas Gregor81d3c042010-11-01 20:13:04 +00004315 if (cursorRange.isInvalid())
4316 return CXChildVisit_Recurse;
4317
Douglas Gregor4419b672010-10-21 06:10:04 +00004318 if (clang_isPreprocessing(cursor.kind)) {
4319 // For macro instantiations, just note where the beginning of the macro
4320 // instantiation occurs.
4321 if (cursor.kind == CXCursor_MacroInstantiation) {
4322 Annotated[Loc.int_data] = cursor;
4323 return CXChildVisit_Recurse;
4324 }
4325
Douglas Gregor4419b672010-10-21 06:10:04 +00004326 // Items in the preprocessing record are kept separate from items in
4327 // declarations, so we keep a separate token index.
4328 unsigned SavedTokIdx = TokIdx;
4329 TokIdx = PreprocessingTokIdx;
4330
4331 // Skip tokens up until we catch up to the beginning of the preprocessing
4332 // entry.
4333 while (MoreTokens()) {
4334 const unsigned I = NextToken();
4335 SourceLocation TokLoc = GetTokenLoc(I);
4336 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4337 case RangeBefore:
4338 AdvanceToken();
4339 continue;
4340 case RangeAfter:
4341 case RangeOverlap:
4342 break;
4343 }
4344 break;
4345 }
4346
4347 // Look at all of the tokens within this range.
4348 while (MoreTokens()) {
4349 const unsigned I = NextToken();
4350 SourceLocation TokLoc = GetTokenLoc(I);
4351 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4352 case RangeBefore:
4353 assert(0 && "Infeasible");
4354 case RangeAfter:
4355 break;
4356 case RangeOverlap:
4357 Cursors[I] = cursor;
4358 AdvanceToken();
4359 continue;
4360 }
4361 break;
4362 }
4363
4364 // Save the preprocessing token index; restore the non-preprocessing
4365 // token index.
4366 PreprocessingTokIdx = TokIdx;
4367 TokIdx = SavedTokIdx;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004368 return CXChildVisit_Recurse;
4369 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004370
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004371 if (cursorRange.isInvalid())
4372 return CXChildVisit_Continue;
Ted Kremeneka333c662010-05-12 05:29:33 +00004373
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004374 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data);
4375
Ted Kremeneka333c662010-05-12 05:29:33 +00004376 // Adjust the annotated range based specific declarations.
4377 const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
4378 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) {
Ted Kremenek23173d72010-05-18 21:09:07 +00004379 Decl *D = cxcursor::getCursorDecl(cursor);
4380 // Don't visit synthesized ObjC methods, since they have no syntatic
4381 // representation in the source.
4382 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
4383 if (MD->isSynthesized())
4384 return CXChildVisit_Continue;
4385 }
Douglas Gregor2494dd02011-03-01 01:34:45 +00004386
4387 SourceLocation StartLoc;
Ted Kremenek23173d72010-05-18 21:09:07 +00004388 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
Douglas Gregor2494dd02011-03-01 01:34:45 +00004389 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
4390 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
4391 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
4392 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
4393 StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
Ted Kremeneka333c662010-05-12 05:29:33 +00004394 }
Douglas Gregor2494dd02011-03-01 01:34:45 +00004395
4396 if (StartLoc.isValid() && L.isValid() &&
4397 SrcMgr.isBeforeInTranslationUnit(StartLoc, L))
4398 cursorRange.setBegin(StartLoc);
Ted Kremeneka333c662010-05-12 05:29:33 +00004399 }
Douglas Gregor81d3c042010-11-01 20:13:04 +00004400
Ted Kremenek3f404602010-08-14 01:14:06 +00004401 // If the location of the cursor occurs within a macro instantiation, record
4402 // the spelling location of the cursor in our annotation map. We can then
4403 // paper over the token labelings during a post-processing step to try and
4404 // get cursor mappings for tokens that are the *arguments* of a macro
4405 // instantiation.
4406 if (L.isMacroID()) {
4407 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding();
4408 // Only invalidate the old annotation if it isn't part of a preprocessing
4409 // directive. Here we assume that the default construction of CXCursor
4410 // results in CXCursor.kind being an initialized value (i.e., 0). If
4411 // this isn't the case, we can fix by doing lookup + insertion.
Douglas Gregor4419b672010-10-21 06:10:04 +00004412
Ted Kremenek3f404602010-08-14 01:14:06 +00004413 CXCursor &oldC = Annotated[rawEncoding];
4414 if (!clang_isPreprocessing(oldC.kind))
4415 oldC = cursor;
4416 }
4417
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004418 const enum CXCursorKind K = clang_getCursorKind(parent);
4419 const CXCursor updateC =
Ted Kremenekd8b0a842010-08-25 22:16:02 +00004420 (clang_isInvalid(K) || K == CXCursor_TranslationUnit)
4421 ? clang_getNullCursor() : parent;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004422
4423 while (MoreTokens()) {
4424 const unsigned I = NextToken();
4425 SourceLocation TokLoc = GetTokenLoc(I);
4426 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4427 case RangeBefore:
4428 Cursors[I] = updateC;
4429 AdvanceToken();
4430 continue;
4431 case RangeAfter:
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004432 case RangeOverlap:
4433 break;
4434 }
4435 break;
4436 }
4437
4438 // Visit children to get their cursor information.
4439 const unsigned BeforeChildren = NextToken();
4440 VisitChildren(cursor);
4441 const unsigned AfterChildren = NextToken();
4442
4443 // Adjust 'Last' to the last token within the extent of the cursor.
4444 while (MoreTokens()) {
4445 const unsigned I = NextToken();
4446 SourceLocation TokLoc = GetTokenLoc(I);
4447 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4448 case RangeBefore:
4449 assert(0 && "Infeasible");
4450 case RangeAfter:
4451 break;
4452 case RangeOverlap:
4453 Cursors[I] = updateC;
4454 AdvanceToken();
4455 continue;
4456 }
4457 break;
4458 }
4459 const unsigned Last = NextToken();
Ted Kremenek6db61092010-05-05 00:55:15 +00004460
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004461 // Scan the tokens that are at the beginning of the cursor, but are not
4462 // capture by the child cursors.
4463
4464 // For AST elements within macros, rely on a post-annotate pass to
4465 // to correctly annotate the tokens with cursors. Otherwise we can
4466 // get confusing results of having tokens that map to cursors that really
4467 // are expanded by an instantiation.
4468 if (L.isMacroID())
4469 cursor = clang_getNullCursor();
4470
4471 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
4472 if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
4473 break;
Douglas Gregor4419b672010-10-21 06:10:04 +00004474
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004475 Cursors[I] = cursor;
4476 }
4477 // Scan the tokens that are at the end of the cursor, but are not captured
4478 // but the child cursors.
4479 for (unsigned I = AfterChildren; I != Last; ++I)
4480 Cursors[I] = cursor;
4481
4482 TokIdx = Last;
4483 return CXChildVisit_Continue;
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004484}
4485
Ted Kremenek6db61092010-05-05 00:55:15 +00004486static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4487 CXCursor parent,
4488 CXClientData client_data) {
4489 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
4490}
4491
Ted Kremenekab979612010-11-11 08:05:23 +00004492// This gets run a separate thread to avoid stack blowout.
4493static void runAnnotateTokensWorker(void *UserData) {
4494 ((AnnotateTokensWorker*)UserData)->AnnotateTokens();
4495}
4496
Ted Kremenek6db61092010-05-05 00:55:15 +00004497extern "C" {
4498
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004499void clang_annotateTokens(CXTranslationUnit TU,
4500 CXToken *Tokens, unsigned NumTokens,
4501 CXCursor *Cursors) {
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004502
4503 if (NumTokens == 0 || !Tokens || !Cursors)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004504 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004505
Douglas Gregor4419b672010-10-21 06:10:04 +00004506 // Any token we don't specifically annotate will have a NULL cursor.
4507 CXCursor C = clang_getNullCursor();
4508 for (unsigned I = 0; I != NumTokens; ++I)
4509 Cursors[I] = C;
4510
Ted Kremeneka60ed472010-11-16 08:15:36 +00004511 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor4419b672010-10-21 06:10:04 +00004512 if (!CXXUnit)
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004513 return;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004514
Douglas Gregorbdf60622010-03-05 21:16:25 +00004515 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004516
Douglas Gregor0396f462010-03-19 05:22:59 +00004517 // Determine the region of interest, which contains all of the tokens.
Douglas Gregor0045e9f2010-01-26 18:31:56 +00004518 SourceRange RegionOfInterest;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004519 RegionOfInterest.setBegin(cxloc::translateSourceLocation(
4520 clang_getTokenLocation(TU, Tokens[0])));
Douglas Gregora8e5c5b2010-07-22 20:22:31 +00004521 RegionOfInterest.setEnd(cxloc::translateSourceLocation(
4522 clang_getTokenLocation(TU,
4523 Tokens[NumTokens - 1])));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004524
Douglas Gregor0396f462010-03-19 05:22:59 +00004525 // A mapping from the source locations found when re-lexing or traversing the
4526 // region of interest to the corresponding cursors.
4527 AnnotateTokensData Annotated;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004528
4529 // Relex the tokens within the source range to look for preprocessing
Douglas Gregor0396f462010-03-19 05:22:59 +00004530 // directives.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004531 SourceManager &SourceMgr = CXXUnit->getSourceManager();
4532 std::pair<FileID, unsigned> BeginLocInfo
4533 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
4534 std::pair<FileID, unsigned> EndLocInfo
4535 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004536
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004537 llvm::StringRef Buffer;
Douglas Gregor0396f462010-03-19 05:22:59 +00004538 bool Invalid = false;
4539 if (BeginLocInfo.first == EndLocInfo.first &&
4540 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) &&
4541 !Invalid) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004542 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4543 CXXUnit->getASTContext().getLangOptions(),
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004544 Buffer.begin(), Buffer.data() + BeginLocInfo.second,
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004545 Buffer.end());
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004546 Lex.SetCommentRetentionState(true);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004547
4548 // Lex tokens in raw mode until we hit the end of the range, to avoid
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004549 // entering #includes or expanding macros.
Douglas Gregor48072312010-03-18 15:23:44 +00004550 while (true) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004551 Token Tok;
4552 Lex.LexFromRawLexer(Tok);
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004553
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004554 reprocess:
4555 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
4556 // We have found a preprocessing directive. Gobble it up so that we
Daniel Dunbar9e1ebdd2010-11-05 07:19:21 +00004557 // don't see it while preprocessing these tokens later, but keep track
4558 // of all of the token locations inside this preprocessing directive so
4559 // that we can annotate them appropriately.
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004560 //
4561 // FIXME: Some simple tests here could identify macro definitions and
4562 // #undefs, to provide specific cursor kinds for those.
4563 std::vector<SourceLocation> Locations;
4564 do {
4565 Locations.push_back(Tok.getLocation());
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004566 Lex.LexFromRawLexer(Tok);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004567 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004568
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004569 using namespace cxcursor;
4570 CXCursor Cursor
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004571 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
4572 Locations.back()),
Ted Kremeneka60ed472010-11-16 08:15:36 +00004573 TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004574 for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
4575 Annotated[Locations[I].getRawEncoding()] = Cursor;
4576 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004577
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004578 if (Tok.isAtStartOfLine())
4579 goto reprocess;
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004580
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004581 continue;
4582 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004583
Douglas Gregor48072312010-03-18 15:23:44 +00004584 if (Tok.is(tok::eof))
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +00004585 break;
4586 }
Douglas Gregor4ae8f292010-03-18 17:52:52 +00004587 }
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004588
Douglas Gregor0396f462010-03-19 05:22:59 +00004589 // Annotate all of the source locations in the region of interest that map to
Ted Kremenekfbd84ca2010-05-05 00:55:23 +00004590 // a specific cursor.
4591 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
Ted Kremeneka60ed472010-11-16 08:15:36 +00004592 TU, RegionOfInterest);
Ted Kremenekab979612010-11-11 08:05:23 +00004593
4594 // Run the worker within a CrashRecoveryContext.
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004595 // FIXME: We use a ridiculous stack size here because the data-recursion
4596 // algorithm uses a large stack frame than the non-data recursive version,
4597 // and AnnotationTokensWorker currently transforms the data-recursion
4598 // algorithm back into a traditional recursion by explicitly calling
4599 // VisitChildren(). We will need to remove this explicit recursive call.
Ted Kremenekab979612010-11-11 08:05:23 +00004600 llvm::CrashRecoveryContext CRC;
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004601 if (!RunSafely(CRC, runAnnotateTokensWorker, &W,
4602 GetSafetyThreadStackSize() * 2)) {
Ted Kremenekab979612010-11-11 08:05:23 +00004603 fprintf(stderr, "libclang: crash detected while annotating tokens\n");
4604 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004605}
Douglas Gregorfc8ea232010-01-26 17:06:03 +00004606} // end: extern "C"
4607
4608//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00004609// Operations for querying linkage of a cursor.
4610//===----------------------------------------------------------------------===//
4611
4612extern "C" {
4613CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
Douglas Gregor0396f462010-03-19 05:22:59 +00004614 if (!clang_isDeclaration(cursor.kind))
4615 return CXLinkage_Invalid;
4616
Ted Kremenek16b42592010-03-03 06:36:57 +00004617 Decl *D = cxcursor::getCursorDecl(cursor);
4618 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
4619 switch (ND->getLinkage()) {
4620 case NoLinkage: return CXLinkage_NoLinkage;
4621 case InternalLinkage: return CXLinkage_Internal;
4622 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
4623 case ExternalLinkage: return CXLinkage_External;
4624 };
4625
4626 return CXLinkage_Invalid;
4627}
4628} // end: extern "C"
4629
4630//===----------------------------------------------------------------------===//
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004631// Operations for querying language of a cursor.
4632//===----------------------------------------------------------------------===//
4633
4634static CXLanguageKind getDeclLanguage(const Decl *D) {
4635 switch (D->getKind()) {
4636 default:
4637 break;
4638 case Decl::ImplicitParam:
4639 case Decl::ObjCAtDefsField:
4640 case Decl::ObjCCategory:
4641 case Decl::ObjCCategoryImpl:
4642 case Decl::ObjCClass:
4643 case Decl::ObjCCompatibleAlias:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004644 case Decl::ObjCForwardProtocol:
4645 case Decl::ObjCImplementation:
4646 case Decl::ObjCInterface:
4647 case Decl::ObjCIvar:
4648 case Decl::ObjCMethod:
4649 case Decl::ObjCProperty:
4650 case Decl::ObjCPropertyImpl:
4651 case Decl::ObjCProtocol:
4652 return CXLanguage_ObjC;
4653 case Decl::CXXConstructor:
4654 case Decl::CXXConversion:
4655 case Decl::CXXDestructor:
4656 case Decl::CXXMethod:
4657 case Decl::CXXRecord:
4658 case Decl::ClassTemplate:
4659 case Decl::ClassTemplatePartialSpecialization:
4660 case Decl::ClassTemplateSpecialization:
4661 case Decl::Friend:
4662 case Decl::FriendTemplate:
4663 case Decl::FunctionTemplate:
4664 case Decl::LinkageSpec:
4665 case Decl::Namespace:
4666 case Decl::NamespaceAlias:
4667 case Decl::NonTypeTemplateParm:
4668 case Decl::StaticAssert:
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004669 case Decl::TemplateTemplateParm:
4670 case Decl::TemplateTypeParm:
4671 case Decl::UnresolvedUsingTypename:
4672 case Decl::UnresolvedUsingValue:
4673 case Decl::Using:
4674 case Decl::UsingDirective:
4675 case Decl::UsingShadow:
4676 return CXLanguage_CPlusPlus;
4677 }
4678
4679 return CXLanguage_C;
4680}
4681
4682extern "C" {
Douglas Gregor58ddb602010-08-23 23:00:57 +00004683
4684enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
4685 if (clang_isDeclaration(cursor.kind))
4686 if (Decl *D = cxcursor::getCursorDecl(cursor)) {
4687 if (D->hasAttr<UnavailableAttr>() ||
4688 (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()))
4689 return CXAvailability_Available;
4690
4691 if (D->hasAttr<DeprecatedAttr>())
4692 return CXAvailability_Deprecated;
4693 }
4694
4695 return CXAvailability_Available;
4696}
4697
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004698CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
4699 if (clang_isDeclaration(cursor.kind))
4700 return getDeclLanguage(cxcursor::getCursorDecl(cursor));
4701
4702 return CXLanguage_Invalid;
4703}
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004704
4705 /// \brief If the given cursor is the "templated" declaration
4706 /// descibing a class or function template, return the class or
4707 /// function template.
4708static Decl *maybeGetTemplateCursor(Decl *D) {
4709 if (!D)
4710 return 0;
4711
4712 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4713 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
4714 return FunTmpl;
4715
4716 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
4717 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
4718 return ClassTmpl;
4719
4720 return D;
4721}
4722
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004723CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
4724 if (clang_isDeclaration(cursor.kind)) {
4725 if (Decl *D = getCursorDecl(cursor)) {
4726 DeclContext *DC = D->getDeclContext();
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004727 if (!DC)
4728 return clang_getNullCursor();
4729
4730 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
4731 getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004732 }
4733 }
4734
4735 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
4736 if (Decl *D = getCursorDecl(cursor))
Ted Kremeneka60ed472010-11-16 08:15:36 +00004737 return MakeCXCursor(D, getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004738 }
4739
4740 return clang_getNullCursor();
4741}
4742
4743CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
4744 if (clang_isDeclaration(cursor.kind)) {
4745 if (Decl *D = getCursorDecl(cursor)) {
4746 DeclContext *DC = D->getLexicalDeclContext();
Douglas Gregor3910cfd2010-12-21 07:55:45 +00004747 if (!DC)
4748 return clang_getNullCursor();
4749
4750 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
4751 getCursorTU(cursor));
Douglas Gregor2be5bc92010-09-22 21:22:29 +00004752 }
4753 }
4754
4755 // FIXME: Note that we can't easily compute the lexical context of a
4756 // statement or expression, so we return nothing.
4757 return clang_getNullCursor();
4758}
4759
Douglas Gregor9f592342010-10-01 20:25:15 +00004760static void CollectOverriddenMethods(DeclContext *Ctx,
4761 ObjCMethodDecl *Method,
4762 llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) {
4763 if (!Ctx)
4764 return;
4765
4766 // If we have a class or category implementation, jump straight to the
4767 // interface.
4768 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
4769 return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods);
4770
4771 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
4772 if (!Container)
4773 return;
4774
4775 // Check whether we have a matching method at this level.
4776 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
4777 Method->isInstanceMethod()))
4778 if (Method != Overridden) {
4779 // We found an override at this level; there is no need to look
4780 // into other protocols or categories.
4781 Methods.push_back(Overridden);
4782 return;
4783 }
4784
4785 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4786 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
4787 PEnd = Protocol->protocol_end();
4788 P != PEnd; ++P)
4789 CollectOverriddenMethods(*P, Method, Methods);
4790 }
4791
4792 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
4793 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
4794 PEnd = Category->protocol_end();
4795 P != PEnd; ++P)
4796 CollectOverriddenMethods(*P, Method, Methods);
4797 }
4798
4799 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
4800 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
4801 PEnd = Interface->protocol_end();
4802 P != PEnd; ++P)
4803 CollectOverriddenMethods(*P, Method, Methods);
4804
4805 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
4806 Category; Category = Category->getNextClassCategory())
4807 CollectOverriddenMethods(Category, Method, Methods);
4808
4809 // We only look into the superclass if we haven't found anything yet.
4810 if (Methods.empty())
4811 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
4812 return CollectOverriddenMethods(Super, Method, Methods);
4813 }
4814}
4815
4816void clang_getOverriddenCursors(CXCursor cursor,
4817 CXCursor **overridden,
4818 unsigned *num_overridden) {
4819 if (overridden)
4820 *overridden = 0;
4821 if (num_overridden)
4822 *num_overridden = 0;
4823 if (!overridden || !num_overridden)
4824 return;
4825
4826 if (!clang_isDeclaration(cursor.kind))
4827 return;
4828
4829 Decl *D = getCursorDecl(cursor);
4830 if (!D)
4831 return;
4832
4833 // Handle C++ member functions.
Ted Kremeneka60ed472010-11-16 08:15:36 +00004834 CXTranslationUnit TU = getCursorTU(cursor);
Douglas Gregor9f592342010-10-01 20:25:15 +00004835 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
4836 *num_overridden = CXXMethod->size_overridden_methods();
4837 if (!*num_overridden)
4838 return;
4839
4840 *overridden = new CXCursor [*num_overridden];
4841 unsigned I = 0;
4842 for (CXXMethodDecl::method_iterator
4843 M = CXXMethod->begin_overridden_methods(),
4844 MEnd = CXXMethod->end_overridden_methods();
4845 M != MEnd; (void)++M, ++I)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004846 (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU);
Douglas Gregor9f592342010-10-01 20:25:15 +00004847 return;
4848 }
4849
4850 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
4851 if (!Method)
4852 return;
4853
4854 // Handle Objective-C methods.
4855 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
4856 CollectOverriddenMethods(Method->getDeclContext(), Method, Methods);
4857
4858 if (Methods.empty())
4859 return;
4860
4861 *num_overridden = Methods.size();
4862 *overridden = new CXCursor [Methods.size()];
4863 for (unsigned I = 0, N = Methods.size(); I != N; ++I)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004864 (*overridden)[I] = MakeCXCursor(Methods[I], TU);
Douglas Gregor9f592342010-10-01 20:25:15 +00004865}
4866
4867void clang_disposeOverriddenCursors(CXCursor *overridden) {
4868 delete [] overridden;
4869}
4870
Douglas Gregorecdcb882010-10-20 22:00:55 +00004871CXFile clang_getIncludedFile(CXCursor cursor) {
4872 if (cursor.kind != CXCursor_InclusionDirective)
4873 return 0;
4874
4875 InclusionDirective *ID = getCursorInclusionDirective(cursor);
4876 return (void *)ID->getFile();
4877}
4878
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004879} // end: extern "C"
4880
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004881
4882//===----------------------------------------------------------------------===//
4883// C++ AST instrospection.
4884//===----------------------------------------------------------------------===//
4885
4886extern "C" {
4887unsigned clang_CXXMethod_isStatic(CXCursor C) {
4888 if (!clang_isDeclaration(C.kind))
4889 return 0;
Douglas Gregor49f6f542010-08-31 22:12:17 +00004890
4891 CXXMethodDecl *Method = 0;
4892 Decl *D = cxcursor::getCursorDecl(C);
4893 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
4894 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
4895 else
4896 Method = dyn_cast_or_null<CXXMethodDecl>(D);
4897 return (Method && Method->isStatic()) ? 1 : 0;
Ted Kremenek40b492a2010-05-17 20:12:45 +00004898}
Ted Kremenekb12903e2010-05-18 22:32:15 +00004899
Ted Kremenek9ada39a2010-05-17 20:06:56 +00004900} // end: extern "C"
4901
Ted Kremenek45e1dae2010-04-12 21:22:16 +00004902//===----------------------------------------------------------------------===//
Ted Kremenek95f33552010-08-26 01:42:22 +00004903// Attribute introspection.
4904//===----------------------------------------------------------------------===//
4905
4906extern "C" {
4907CXType clang_getIBOutletCollectionType(CXCursor C) {
4908 if (C.kind != CXCursor_IBOutletCollectionAttr)
Ted Kremeneka60ed472010-11-16 08:15:36 +00004909 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
Ted Kremenek95f33552010-08-26 01:42:22 +00004910
4911 IBOutletCollectionAttr *A =
4912 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
4913
Ted Kremeneka60ed472010-11-16 08:15:36 +00004914 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
Ted Kremenek95f33552010-08-26 01:42:22 +00004915}
4916} // end: extern "C"
4917
4918//===----------------------------------------------------------------------===//
Ted Kremenek04bb7162010-01-22 22:44:15 +00004919// Misc. utility functions.
4920//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00004921
Daniel Dunbarabdce7a2010-11-05 17:21:46 +00004922/// Default to using an 8 MB stack size on "safety" threads.
4923static unsigned SafetyStackThreadSize = 8 << 20;
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00004924
4925namespace clang {
4926
4927bool RunSafely(llvm::CrashRecoveryContext &CRC,
Ted Kremenek6c53fdd2010-11-14 17:47:35 +00004928 void (*Fn)(void*), void *UserData,
4929 unsigned Size) {
4930 if (!Size)
4931 Size = GetSafetyThreadStackSize();
4932 if (Size)
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +00004933 return CRC.RunSafelyOnThread(Fn, UserData, Size);
4934 return CRC.RunSafely(Fn, UserData);
4935}
4936
4937unsigned GetSafetyThreadStackSize() {
4938 return SafetyStackThreadSize;
4939}
4940
4941void SetSafetyThreadStackSize(unsigned Value) {
4942 SafetyStackThreadSize = Value;
4943}
4944
4945}
4946
Ted Kremenek04bb7162010-01-22 22:44:15 +00004947extern "C" {
4948
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00004949CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00004950 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00004951}
4952
4953} // end: extern "C"